Link to home
Start Free TrialLog in
Avatar of jmingo
jmingo

asked on

str_replace question

I want to do a replace in a string everytime there is, for example a

http://www.webserveraddress.com OR
www.myserver.com

i want it to replace this with

<a href=\"http://www.webserveraddress.com\">http://www.webserveraddress.com</a>

thanks
Avatar of Batalf
Batalf
Flag of United States of America image

Try

<?

$string = "http://www.webserveraddress.com";
$string = "<a href=\"$string\">$string</a>";

?>

Avatar of jmingo
jmingo

ASKER

the string could be a lot of text, its being taken out of a database.... for example

Hi Bill

Check out this great link, good website, blah blah blah

http://www.cnn.com

thanks,
Jimmy
Ok, then try

<?

$string = "This is a test on http://www.webserveraddress.com so check it out and this is another url http://www.adomain.com end text";
$string = preg_replace("/(http:\/\/.*?(\s|$))/si","<a href=\"\\1\">\\1</a>",$string);
echo htmlentities($string); // THIS LINE IS JUST FOR DEBUGGING

?>

Avatar of jmingo

ASKER

thanks i'll try that.... what if they put in www.cnn.com instead of http://www.whatever.com ???
Doesn't matter. It searches for substrings that starts with "http://"
Avatar of jmingo

ASKER

if the string contains these

http://www.cnn.com 

www.microsoft.com

only the cnn links.
that's because you don't have http:// in front of it www.microsoft.com.

You can try this one:


<?

$string = "This is a test on http://www.webserveraddress.com so check it out and this is another url www.adomain.com";
$string = preg_replace("/(http:\/\/.*?(\s|$))/si","<a href=\"\\1\">\\1</a>",$string);
$string = preg_replace("/(\swww\..*?(\s|$))/si","<a href=\"http://\\1\">\\1</a>",$string);
echo htmlentities($string);

?>

Batalf
Avatar of jmingo

ASKER

i have this code below and the www link doesn't even show up...?? any ideas. thanks for all your help.

$content1 = preg_replace("/(\swww\..*?(\s|$))/si","<a href=\"http://\\1\" target=\"_blank\">\\1</a>",$content1);

$content1 = preg_replace("/(http:\/\/.*?(\s|$))/si","<a href=\"\\1\" target=\"_blank\">\\1</a>",$content1);
Try changing

$content1 = preg_replace("/(http:\/\/.*?(\s|$))/si","<a href=\"\\1\" target=\"_blank\">\\1</a>",$content1);

to

$content1 = preg_replace("/(\shttp:\/\/.*?(\s|$))/si","<a href=\"\\1\" target=\"_blank\">\\1</a>",$content1);
Avatar of jmingo

ASKER

www.microsoft.com opens up a browser but the link is this

http://%20www.microsoft.com/

which doesn't work...
Use these and use them in that specific order:

$content1= preg_replace("/(\http:\/\/.*?(\s|$))/si","<a href=\"\\1\">\\1</a>",$content1);
$content1= preg_replace("/(\s)(\www\..*?(\s|$))/si","\\1<a href=\"http://\\2\">\\2</a>",$content1);
Avatar of jmingo

ASKER

excellent works great!!!

what about something like this... i don't think i need to worry about it, outlook and messenger and other microsoft products don't worry about it

http://www.wherever.com/My Files/index.html

the space.
What's after the space won't be a part of the link. As you can see, this is how it works here at EE also(look at your link in the previous post)

Batalf
Avatar of jmingo

ASKER

hahaa yeah.. ok

in firefox is seems to be turning them into this

www.microsoft.com%20 could not be found. Please check the name and try again
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmingo

ASKER

perfect, thanks so much
Glad I could help