vendredi 1 juillet 2016

php regex replace local relative pathways

I have multiple relative links that are included in a string. However I want too have all the links replace with a simple javascript:alert("Sorry You Cannot Do That.") and I do not want any absolute pathways such as http://google.com to change only the relative pathways such as /conf/bin.html.

Here is an example code snippet:

$pattern = "/<a(.*) href='/(.*)'(.*)>reply</a>/";
$string = "<a target='_blank' href='/conf/bin?post=5760627b29ba0' name='bin' id='bin' class='bin' title='Hide From Feed'></a>
wow
<a target='_blank' href='/conf/bin?post=5760627b29ba0' name='bin' id='bin' class='bin' title='Hide From Feed'></a> wow";
while (preg_match($pattern, $string)){

$string = preg_replace($pattern, "<tr><td align='right'><a href='javascript:alert(" . chr(34) . "Sorry You Cannot Do That" . chr(34) . ")' style='text-decoration:none;'>reply</a>", $string);
}

And I want the string to end up as:

$string = "<a target='_blank' href='javascript:alert(" . chr(34) . "Sorry You Cannot Do That" . chr(34) . ")' name='bin' id='bin' class='bin' title='Hide From Feed'></a>
wow
<a target='_blank' href='javascript:alert(" . chr(34) . "Sorry You Cannot Do That" . chr(34) . ")' name='bin' id='bin' class='bin' title='Hide From Feed'></a> wow";

Can anyone help. Thanks

Aucun commentaire:

Enregistrer un commentaire