Home >Backend Development >PHP Problem >How to replace a certain value in a link in php
php method to replace a certain value in the link: 1. Create a PHP sample file; 2. Replace the href attribute value in the a tag through the "function a_replace_href($str) {...}" method. Can.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
How to replace the link in php a certain value?
php Replace the href attribute of the a tag in html
The data collected when imitating the site contains many a links that can jump to other sites, which can be replaced during collection Drop
<?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, $matchs); return $matchs[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);
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to replace a certain value in a link in php. For more information, please follow other related articles on the PHP Chinese website!