"Emergency" Regular or class, PHP to replace the non-site domain name

Source: Internet
Author: User
A forum program, the link in the user post (* Not the site's domain name of the link) all replaced by the specified connection


For example, source code:


-------------------------------------------------
Aaaaabbbbbcccccdddddeeeeefffffggggg


-----------------------------------------------------------
Replaced by

-----------------------------

Aaaaabbbbbcccccdddddeeeeefffffggggg


------------------------


Requirement: Replace all links under non-aaa.com domain name with "#" pound sign

The following code can replace all links as the pound sign, but cannot tell if it is aaa.com
echo Preg_replace ("/(? <=href=) ([^\>]*) (?=\>)/I", "javascript:;", "Hello, click here to see Hello, click here to see");

Ask the great God help ha very anxious not to get out of the morning


Reply to discussion (solution)

$str = <<
  
  

Replace it with the drop thank you ha

Aaaaabbbbcccccdddddeeeeefffffggggg

echo Preg_replace_callback ("/([^<]+) <\/a>/is", function ($r) {return strip_tags ($r [0]);}, $STR);

Thanks microlab2009
But in the application, I tested it and found a few problems.


1: Replace the non-site chain replacement of the pound, but the link is also replaced Oh!
For example: and www.163.com (string non-link) are all replaced by the # number

2: Delete link
bbbbb
And
bbbbb
Error on processing, can modify the first one cannot modify the second one.

Hope the great God to help Ah, infinite gratitude has been Chung centripetal mouth ....

1. $str = <<
AAAAA
bbbbb
Ccccc
Ddddd
Eeeee
FFFFF
Ggggg
Http://234r.231.com
Str

echo Preg_replace_callback ("/(? <=href\=\") http\:\/\/[^.] *[^a]+\.com\//is ", function ($r) {return str_replace ($r [0]," # ", $r [0]);}, $STR);
?>

2.echo Preg_replace_callback ("/]*> ([^<]+) <\/a>/is", function ($r) {return strip_tags ($r [0]);}, $STR);
You just have to change it, or you have to go ahead and get familiar with the regular.

Thank you very much for the help of God, ha ~ ~ ~

  • Related Article

    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.