Home > Backend Development > PHP Tutorial > PHP batch delete hyperlinks in web content_PHP tutorial

PHP batch delete hyperlinks in web content_PHP tutorial

WBOY
Release: 2016-07-13 10:44:55
Original
1015 people have browsed it

When building a content website, I often use collection software to invade the resources of other sites on the Internet. I collect thousands of articles. After collecting, I find that there are some hyperlinks to the original site in the content. If I go there one by one It would be troublesome to change it, so I wrote a method and the test was successful.

Let’s briefly talk about the principle. What is reused here is PHP’s replacement function preg_replace. In practical applications, we often use preg_replace to replace some dangerous characters or convert some slashes or carriage returns, etc. preg_replace($1,$2,$3) has three important parameters, where $1 is the string to be searched, $2 is the string to be replaced, and $3 is the string to be replaced.

Now that we know how the preg_replace function works, it is not difficult to replace hyperlinks. We only need to convert the parameters $1 and $2 into arrays and perform batch replacement. The following is the method. The test is successful and shared with phper.

The code is as follows
 代码如下 复制代码

$str="超级链接|这是个链接
";
function removelink($str){
$mode=array("##iUs","##iUs");
$want=array("","");
$con=preg_replace($mode,$want,$str);
return $con;
}
echo removelink($str);
?>

Copy code

 代码如下 复制代码
$content = file_get_contents('test.html');
$url = 'http://www.hzhuti.com'; //要换成的新网址
$preg = '/[s]href=("|')[S]*("|')/i';
$replace = ' href="' . $url . '"';
$content = preg_replace($preg, $replace, $content); //正则替换
create_log('newhtml', $content); //生成新文件
?>
$str="Hyperlink|This is a link
";
function removelink($str){
$mode=array("##iUs","##iUs");
$want=array("","");
$con=preg_replace($mode,$want,$str);
return $con;
}
echo removelink($str);
?>

or

Everything is possible http://www.bkjia.com/PHPjc/633069.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/633069.html
TechArticle
When building a content site, collection software is often used to invade the resources of other sites on the Internet. Once collected, There are thousands of articles. After collecting them, I found that there are some super links to the original site in the content...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template