Replacing text...
Posted: Sun Jan 28, 2007 11:20 am
I have this:
And I want PHP to replace <a href="http://site.com/contact.htm"> with [contact] and the </a> following it with [/contact]
And then <a href="http://site.com/directions.htm"> with [address] and the </a> following it with [/address]
I have a code in Javascript to do the opposite:
How could I make it do the same thing in PHP, but reverse replacing the directiosn link with [address] text [/address] and the contact links with [contact] text [/contact]
I have this, but it gets contact and address messed up...
______________________________________________________________________________
I know how this code:
But it's still not working the only thing it does right is the <p> and tab stuff.
Code: Select all
<a href="http://site.com/contact.htm">Contact</a> | <a href="http://site.com/directions.htm">Directions</a> And I want PHP to replace <a href="http://site.com/contact.htm"> with [contact] and the </a> following it with [/contact]
And then <a href="http://site.com/directions.htm"> with [address] and the </a> following it with [/address]
I have a code in Javascript to do the opposite:
Code: Select all
str = str.replace(/\[address\](.*?)\[\/address\]/gi, "<a href=\"http://site.com/directions.htm\">$1</a>");
I have this, but it gets contact and address messed up...
Code: Select all
$get_mess = "<a href="http://site.com/contact.htm">Contact</a> | <a href="http://site.com/directions.htm">Directions</a>";
$get_mess = str_replace("<a href=\"http://site.com/contact.htm\">", "[contact]", $get_mess);
$get_mess = str_replace("</a>", "[/contact]", $get_mess);
$get_mess = str_replace("<a href=\"http://site.com/directions.htm\">", "[address]", $get_mess);
$get_mess = str_replace("</a>", "[/address]", $get_mess);I know how this code:
Code: Select all
$get_mess = str_replace("<p>", "[p]", $get_mess);
$get_mess = str_replace(" ", "[tab]", $get_mess);
$patterns[0] = '/<a href="http:\/\/site\.com\/directions.htm">(.*?)<\/a>/i';
$patterns[1] = '/<a href="http:\/\/site\.com\/contact.htm">(.*?)<\/a>/i';
$replacements[0] = '[address]$1[/address]';
$replacements[1] = '[contact]$1[/contact]';
preg_replace($patterns, $replacements, $get_mess);
$out_message = $get_mess;