Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Change link with preg_replace_callback

Last post 04-30-2008, 4:23 AM by everisk. 0 replies.
Sort Posts: Previous Next
  •  04-30-2008, 4:23 AM 41766

    Change link with preg_replace_callback

    Hi,

     I have a the below code which seach for all links and it works fine

    $url = "some url";
    $input = @file_get_contents($url) or die('Could not access file: $url');
    $regexp = "href=(\"??)([^\" >]*?)\\1[^>]*";

    if(preg_match_all("/$regexp/siU", $input, $matches)) {
    # $matches[2] = array of link addresses
    # $matches[3] = array of link text - including HTML code
        print_r($matches[2]); // print all the links that are found in the format -- http://www.url.com/somethinghere/link?abc=123
    }

     However, when I use preg_replace_callback to modify the link, it doesn't work. Below is my code

    function convert_link($matches)
    {
        //print "more code".$matches[2]."<br>"; //output the correct url in the correct format -- more code http://www.url.com/somethinghere/link?abc=123
        return "more code".$matches[2];
    }
    echo preg_replace_callback("/$regexp/siU", "convert_link", $input); //get something like <a target="_blank" more="" codehttp:="" www.url.com="" somethinghere="" link?abc=123>


     Please help [:'(]

View as RSS news feed in XML