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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.