PHP removes the regular expression of the string hyperlink text

Source: Internet
Author: User
Tags html tags regular expression

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.111cn.net otherwise the consequences are at your own risk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.