Search code examples
phpregexhref

take a href links and add it to variables using regex


im new in php and regex.

I have html code from the old apps:

$oldlinkshtml = "<li class="active"><a href="www.example.com?page=1">Previous</a></li>";
// desired output
$oldlinksvar = "";

echo $oldlinksvar;

And i want to remove all the html tag in $oldlinkshtml and only take "www.example.com?page=1" and add it to $oldlinksvar. How can i do that using regex? please help me.


Solution

  • preg_replace('/<a(.*?)href="([^"]*)"(.*?)>/','<a $1 href="http://new.href/?old_url=$2" $3>',$text);
    


    Input: <li class="active"><a href="www.example.com?page=1">Previous</a></li>
    Output: <li class="active"><a href="http://new.href/?old_url=www.example.com?page=1" >Previous</a></li>