PHP HREF attribute a label to replace all of the HTML file, the other unchanged

Reprinted Source: http://www.luanxin.top/index.php/archives/21/

When picking down imitation station code a link always points to somewhere else, to a change of a word to be exhausted, when the show just tap on the bounce, bored, so would like to write a change with a php Code link href attribute code is as follows:

<?php // 替换a标签中的href属性值
function a_replace_href($str) {
  $preg = '/href=(\"|\')(.*?)(\"|\')/i';
  $replacestr = 'href="#"';
  $res = preg_replace($preg, $replacestr, $str);
  return $res;
}
// 获取字符串中的所有a标签
function from_str_get_a($str){
    $preg = '/<a .*?>/i';
    preg_match_all($preg, $str, $imgArr);
    return $imgArr[0];
}
$str = file_get_contents("index.html");
$data = from_str_get_a($str);//获取字符串中的a标签
$data1 = a_replace_href($data);//替换后应该显示的a标签
$res = $str;
// 反复对字符串进行查找替换
foreach ($data as $key => $value) {
    $res = str_replace($value,$data1[$key],$res,$i);
}
// 另存文件
file_put_contents("index1.html",$res);
?>

Principle is to use the regular match and replace, if you have a better idea or optimize welcomed leave comments, share your experience!
Test results are as follows:
Source Code Files:
Sogou Screenshot 19 August 28 1951_1.png
Replace the file code:

Guess you like

Origin www.cnblogs.com/luanxin/p/11426207.html