0
votes

i have html and contain many links like this:

<a href="http://www.mydomain1.com">Click Me</a>

i would like to:

copy its original link: http://www.mydomain1.com

append text infront it: test.php?url=http://www.mydomain1.com

so that the final output for all href to become:

<a href="test.php?url=http://www.mydomain1.com">Click Me</a>

how to do this?

1
do not parse HTML with regex. use DOM parser instead.trainoasis
do you have any example?Teddybugs

1 Answers

1
votes

It is usually not a good practice to use regex on HTML. Use DOM parser instead:

If you really need to, you can use my regex from this answer:

$regexForHREF = "/href[ ]*=[ ]*(?:\"|')(.+?)(?:\"|')/";

to get the href value and then you can use str_replace to add your "test.php?url=".