Due to some bad actions performed a long time ago, some junk data exists on the website content page. This morning, I added a method to remove the text of the string hyperlink. Next I will use the regular expression to process it.
The functionality of the following instance is to filter all html tags and replace all text before the h1-h5
| The Code is as follows: |
Copy code |
|
For ($ I = 1; $ I <= 5; $ I ++)
{
$ SQL = "SELECT * FROM 'table name 'where' field 'like'
$ Query = mysql_query ($ SQL) or die (mysql_error ());
If (mysql_num_rows ($ query ))
{
While ($ rs = mysql_fetch_array ($ query ))
{
// Print_r ($ rs );
$ T = stripslashes ($ rs ['field']);
$ Str = nl2br (strip_tags (addslashes (removelink ($ t ))));
$ SQL = "update table name set field = '$ str 'where id =". $ rs ['id'];
If (mysql_query ($ SQL ))
{
Echo $ rs ['id']. 'success <br/> ';
}
Else
{
Echo mysql_error ();
}
}
}
Else
{
Echo '. $ SQL.' <br/> 'has been updated ';
}
}
Function removelink ($ t)
{
// $ Str = preg_replace ("/<a [^>] * href = [^>] *> | </[^ a] * a [^>] *>/ I ", "", $ t );
$ Str = preg_replace ("/(? Is )(? <= <H1> ).*? (? = </H1>)/I "," ", $ t );
$ Str = preg_replace ("/(? Is )(? <= <H2> ).*? (? = </H2>)/I "," ", $ str );
$ Str = preg_replace ("/(? Is )(? <= <H3> ).*? (? = </H3>)/I "," ", $ str );
$ Str = preg_replace ("/(? Is )(? <= <H4> ).*? (? = </H4>)/I "," ", $ str );
$ Str = preg_replace ("/(? Is )(? <= <H5> ).*? (? = </H5>)/I "," ", $ str );
Return re_h ($ str );
}
Function re_h ($ str)
{
$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('$ Str = str_replace ('Return $ str;
} |
The following regular expression is used.
| The Code is as follows: |
Copy code |
|
Preg_replace ("/(? Is )(? <= <H1> ).*? (? = </H1>)/I "," ", $ t ); |
This is the core code.
For example, if you want to remove the hyperlink content from the text, you need to use a regular expression. For example, you can use $ str = preg_replace ("/<a [^>] * href = [^>] *> | </[^ a] * a [^>] * >/I ", "", $ strhtml); this section is used to implement the requirements. For more solutions, see the following.
1. Delete the hyperlink in the content
| The Code is as follows: |
Copy code |
|
Ereg_replace ('<a ([^>] *)> ([^ <] *) </a> ', '<font color = "red"> \ 2 </font>', $ content );
Ereg_replace ("<a [^>] * >|</a>", "", $ content ); |
2. Remove hyperlinks containing specific words
| The Code is as follows: |
Copy code |
|
$ Find = "this string is my find ";
$ String = '<font color = "red"> replaced </font>'; // the content of the hyperlink.
Echo ereg_replace ('<a ([^>] *)> ([^ <] *'. $ find. '[^>] *) </a>', '<font color = "red"> \ 2 </font>', $ content ); |
Original site, reprinted must indicate the source www. bKjia. c0m otherwise the consequences are at your own risk