PHP Regular expression _php for removing string hyperlink text tutorial

Source: Internet
Author: User
Because N long ago did some very bad action caused the site content page has some garbage data, this morning, the whole of a string to remove the hyperlink text method, I combine the regular to deal with.

The function of the following instance is to filter all HTML tags and replace all text before 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
';
}
Else
{
Echo Mysql_error ();
}
}
}
Else
{
Echo ' has been updated with no record '. $sql. '
';
}

}


function Removelink ($t)
{
$str = Preg_replace ("/]*href=[^>]*>| ]*>/i "," ", $t);

$str = Preg_replace ("/(? is) (? <=

).*? (?=

)/I "," ", $t);
$str = Preg_replace ("/(? is) (? <=

).*? (?=

)/I "," ", $str);
$str = Preg_replace ("/(? is) (? <=

).*? (?=

)/I "," ", $str);
$str = Preg_replace ("/(? is) (? <=

).*? (?=

)/I "," ", $str);
$str = Preg_replace ("/(? is) (? <=

).*? (?=

)/I "," ", $str);

Return Re_h ($STR);
}

function Re_h ($STR)
{
$str = Str_replace ('

', ', $str);
$str = Str_replace ('

', ', $str);
$str = Str_replace ('

', ', $str);
$str = Str_replace ('

', ', $str);
$str = Str_replace ('

', ', $str);
$str = Str_replace ('

', ', $str);
$str = Str_replace ('', ', $str);
$str = Str_replace ('', ', $str);
$str = Str_replace ('', ', $str);
$str = Str_replace ('', ', $str);
return $str;
}

It uses the following regular expression

The code is as follows Copy Code

Preg_replace ("/(? is) (? <=

).*? (?=

)/I "," ", $t);

This is the core code.


For example, you need to remove the content of hyperlinks in the text, you need to use regular expressions. For example you can use $STR = Preg_replace ("/]*href=[^>]*>| ]*>/i "," ", $strhtml); This section to achieve the requirements, if you want more solutions, you can see the following.

1. Delete the hyperlinks in the content

The code is as follows Copy Code

Ereg_replace (']*) > ([^<]*) ', ' \2 ', $content);

Ereg_replace ("]*>|", "", $content);


2. Eliminate hyperlinks that contain specific words

The code is as follows Copy Code

$find = "This string is my find";
$string = ' replaced ';//To replace the hyperlink with the content
Echo ereg_replace (']*) > ([^<]* '. $find. ' [^>]*] ', ' \2 ', $content);

Original site, reprint must indicate the source www.bKjia.c0m otherwise the consequences of self-esteem

http://www.bkjia.com/PHPjc/633130.html www.bkjia.com true http://www.bkjia.com/PHPjc/633130.html techarticle because N long ago did some very bad action caused the site content page has some garbage data, this morning, the whole of a string to remove the hyperlink text method, the following I combine the regular to ...

  • 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.