PHP Replacement function Preg_replace usage notes

Source: Internet
Author: User

Today should be the boss's needs, will not be our page to modify a link, with js+iframe should also be able to achieve, but I want to try the PHP implementation method.

First you have to download someone else's page into your PHP, the implementation method can be used curl, file, here is a good article written http://www.11jn.com/phpbb/viewtopic.php?f=31&t= 1390, there is not much to say.

Then just use regular expression to find your link, because it is a specific link, directly write, such as Baidu (http\:\/\/www.baidu.com)


Here is the main function preg_replace ()

Mixed preg_replace (mixed $pattern,     mixed $replacement,   mixed $subject     int $limit =1
   
    int & $count]])
   

The replacement of the regular expression (to be replaced) needs to match the substituted object Optionally, specify the number of replacements, and if the limit is omitted or the value is-1, all occurrences will be replaced

To replace an instance of:

To replace the www.baidu.com with www.google.com.

$content= ' http://www.baidu.com '; $replace=preg_replace("(http\:\/\/www.baidu.com)", "http://www.google.com",$content ); Echo $replace;

Second instance of substitution:

Replace two multiple fields in one paragraph, replace www.baidu.com with www.google.com, and Windows replaces Linux

$content = "http://www.baidu.com on Windows.";
$str 1=array ("(http\:\/\/www.baidu.com)", "(Windows)");
$str 2=array ("http://www.google.com", "Linux");
$replace =preg_replace ($str 1, $str 2, $content);
Echo $replace;

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.