Jump to content

preg_replace


headmine

Recommended Posts

I have a string of Data. Inside this string there is a line of text that says

 

Where: 1234 Main Street.

 

What I would like to do is grab the address that comes after "Where:" and have the output with a link to google maps

 

ex

 

Where: <a href="http://maps.google.com?q=1234+Main+Street">1234 Main Street</a>

 

Would I use preg_replace for this? Or am I attacking this the wrong way?

 

Link to comment
https://forums.phpfreaks.com/topic/206345-preg_replace/
Share on other sites

$str = preg_replace('/Where: ([^\n]*)/m','Where: <a href="http://maps.google.com?q=$1">$1</a>', $str );

 

that will leave spaces in the url.

 

IF you need the + in there then:

 

preg_match('/Where: ([^\n]*)/m', $str , $matches );
$term = str_replace(' ', '+', $matches[1]);
$str = preg_replace('/Where: ([^\n]*)/m','Where: <a href="http://maps.google.com?q='.$term.'">$1</a>', $str );

 

Link to comment
https://forums.phpfreaks.com/topic/206345-preg_replace/#findComment-1079436
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.