During content stations, collection software is often used to attack other station resources on the Internet, and thousands of articles are collected, after the collection, we found that there were some original site hyperlinks in the content. If I had to modify them one by one, I had to write a method and the test was successful.
Let's briefly describe the principle. Here we reuse the PHP replacement function preg_replace. In practice, we often use preg_replace to replace some dangerous characters or convert some slashes or carriage returns. Preg_replace ($1, $2, $3) has three important parameters. $1 is the string to be searched, and $2 is the string to be replaced, $3 is the string to be replaced.
Now that we know the working principle of the preg_replace function, it is not difficult to replace the hyperlink. We only need to convert the parameters $1 and $2 into arrays and replace them in batches. The following are the methods, the test is successful and shared to phper.
| The Code is as follows: |
Copy code |
|
<? Php
$ Str = "<a href =" xxx "> hyperlink </a> | <a href =" xxx "> This is a link </a> <br> ";
Function removelink ($ str ){
$ Mode = array ("# <a href =" (. *) "> # iUs", "# </a> # iUs ");
$ Want = array ("","");
$ Con = preg_replace ($ mode, $ want, $ str );
Return $ con;
}
Echo removelink ($ str );
?> |
Or
| The Code is as follows: |
Copy code |
<? Php
$ Content = file_get_contents('test.html ');
$ Url = 'HTTP: // www.hzhuti.com '; // The new url to be replaced
$ Preg = '/[s] href = ("|') [S] * (" | ')/I ';
$ Replace = 'href = "'. $ url .'"';
$ Content = preg_replace ($ preg, $ replace, $ content); // Regular Expression replacement
Create_log ('newhtml ', $ content); // generate a new file
?>
|
Yes.