Replacing text...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Replacing text...

Post 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.
Last edited by tecktalkcm0391 on Sun Jan 28, 2007 11:50 am, edited 3 times in total.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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;
Post Reply