Page 1 of 1

Replacing text...

Posted: Sun Jan 28, 2007 11:20 am
by tecktalkcm0391
I have this:

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>");
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...

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("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", "[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;
But it's still not working the only thing it does right is the <p> and tab stuff.

Posted: Sun Jan 28, 2007 11:44 am
by iknownothing
As far as I can tell, it cant find "<a href=\"http://sitename.com/contact.htm\">", because it doesnt exist, only "<a href="contact.htm">" exists, and the same goes for the directions link

Posted: Sun Jan 28, 2007 11:48 am
by tecktalkcm0391
well the link is really <a href="http://site.com/contact.htm"> I fixed the top for other people. And changed the "new" code just a tab.

UPDATE: I am feel stupid :oops::

Code: Select all

$get_mess = str_replace("<p>", "[p]", $get_mess); 
        $get_mess = str_replace("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", "[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]'; 
        $get_mess =  preg_replace($patterns, $replacements, $get_mess);  // this line wasn't saved to $get_mess
        $out_message = $get_mess;