0

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?

Teddybugs
  • 1,232
  • 1
  • 12
  • 37

1 Answers1

1

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=".

Community
  • 1
  • 1
trainoasis
  • 6,419
  • 12
  • 51
  • 82