Click to See Complete Forum and Search --> : string replace


jrthor2
03-17-2005, 06:33 AM
I have a bunch of text that comes from a database, and in that text there may be an email address. How can I parse through the text, and if there is an email address, make it a mailto link?

Thanks

[B]UPDATE[B]
I have the following code:


$event_info = trim($_additional_info);
$event_info = str_replace("<event_type>", $_name, $event_info);
$event_info = str_replace("<event_start_date>", $_start_date, $event_info);
$event_info = str_replace("<event_end_date>", $_end_date, $event_info);

$event_info = str_replace("<event_start_time>", $_start_time, $event_info);
$event_info = str_replace("<event_end_time>", $_end_time, $event_info);
$event_info = htmlspecialchars($event_info);
$event_info = preg_replace('#(.*)\@(.*)\.(.*)#','<a href="mailto:\\1@\\2.\\3">\\1@\\2.\\3</a>',$event_info);


The Result that comes back is:

Join the fight by taking steps toward a cure. America's Walk for Diabetes is coming to Twin Lakes Park in Greensburg, PA., on September 25, 2005. Walk as an individual or by forming a team of family, friends and co-workers. To sign up or for more information on the event near you, contact Betty Brdar at bbrdar@diabetes.org or (412)824-1181 x4520. Click here for more information.

The entire sentence with the email address bbrdar@diabetes.org is made a link, and the mailto address is the entire sentence???

mikko
03-17-2005, 07:03 AM
Maybe with preg_replace instead of str_replace...

A bit like this:
function parseEmail($input) {

$input = preg_replace("/([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+/i", "<a href=\"mailto:\\0\">\\0</a>", $input);

return $input;
}

Edit: Didn't see your update... But mine seems to work correctly. Here's a test: http://www.eternityproject.net/exp/parsemail.php

jrthor2
03-17-2005, 07:08 AM
Almost, I used your code and got this (notice the email address is missing after the persons name:

Join the fight by taking steps toward a cure. America's Walk for Diabetes is coming to Twin Lakes Park in Greensburg, PA., on September 25, 2005. Walk as an individual or by forming a team of family, friends and co-workers. To sign up or for more information on the event near you, contact Betty Brdar at or (412)824-1181 x4520. Click here for more information.

here's my code:

$event_info = preg_replace("/([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+/i", "<a href=\"mailto:\0\">\0</a>", $event_info);

mikko
03-17-2005, 07:10 AM
Yeah, just noticed that the forum stripped slashes from the preg_replace...

It's supposed to be

preg_replace("/([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+/i", "<a href=\"mailto:\\0\">\\0</a>", $input);

jrthor2
03-17-2005, 07:12 AM
Yep, that worked.

Thanks!!

jrthor2
03-17-2005, 07:30 AM
how would I do the same thing with a link? In my text, I may have www.yoursite.com. I would need to replace that with <a href="http://www.yoursite.com">www.yoursite.com</a>

Thanks again!

mikko
03-17-2005, 07:40 AM
// Parse links starting with http://, check that the link doesn't start with href="...
$input = preg_replace("/(?<!href=\")(http:\/\/(\d|\w|[._-]|\/)*)/i", "<a href=\"\\0\">\\0</a>", $input);

// Parse links starting with www., check that the link isn't already parsed.
$input = preg_replace("/(?<!http:\/\/)(www\.(\d|\w|[._-]|\/)*)/i", "<a href=\"http://\\0\">\\0</a>", $input);

Can't really post with code tags... all backslashes are gone =(

That code isn't bulletproof, though, but seems to be working.
http://www.eternityproject.net/exp/parsemail.php