1

Earlier today I asked a question about having regex solution for replacing hrefs of all links in html with return fo some function..

Then I deleted it because it seemed I could work around with it using DomDocument, but It turns out, I can't...

See my html has got lots of wrong html syntex, html5 code etc, etc..So neither loadHTML, loadXML is working..

Please if someone can provide solution with regex to look-for href's and replace using calling a function on all..I've tried preg_replace, and preg_replace_callback, but I can't understand the $1, $2..whole concept.. I've searched all similar questions, but they all have answers of DomDocumenet, which I can't use..

Old similar questions: Grabbing the href attribute of an A element

Find all hrefs in page and replace with link maintaining previous link - PHP

but I need regex solution

Community
  • 1
  • 1
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56

2 Answers2

1

Try this code:

// $text - your text
// $1 - preg variable for 1st "(.*)" match
// $2 - preg variable for 2nd "(.*)" match
preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a $1 href="http://new.href" $3>',$text);

I paste working example here: http://codepad.org/KORtcb9q

StasGrin
  • 1,800
  • 2
  • 14
  • 30
1

I created one..the main problem usually people afraid are having other attributes, before href, after href, spaces, having wrong url specification, href url in single or double quotes, etc..The big advantage is that I learned how to use them..

$callback = function ($match) use($params){
        $data = MyClass::updateUrl($match[2], $params);
        $return_data = 'href='.$match[1].$data.$match[1];
        return $return_data;
    };
    $reg = "#href\s*=\s*(\"|')?([^\"'>]+)(\"|')#i";
    return preg_replace_callback($reg, $callback, $html);
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56