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
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
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
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>",$str ing);
echo htmlentities($string); // THIS LINE IS JUST FOR DEBUGGING
?>
<?
$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>",$str
echo htmlentities($string); // THIS LINE IS JUST FOR DEBUGGING
?>
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://"
ASKER
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>",$str ing);
$string = preg_replace("/(\swww\..*? (\s|$))/si ","<a href=\"http://\\1\">\\1</a>",$string) ;
echo htmlentities($string);
?>
Batalf
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>",$str
$string = preg_replace("/(\swww\..*?
echo htmlentities($string);
?>
Batalf
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 );
$content1 = preg_replace("/(\swww\..*?
$content1 = preg_replace("/(http:\/\/.*?(\s|$))/si","<a href=\"\\1\" target=\"_blank\">\\1</a>"
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 );
$content1 = preg_replace("/(http:\/\/.*?(\s|$))/si","<a href=\"\\1\" target=\"_blank\">\\1</a>"
to
$content1 = preg_replace("/(\shttp:\/\/.*?(\s|$))/si","<a href=\"\\1\" target=\"_blank\">\\1</a>"
ASKER
www.microsoft.com opens up a browser but the link is this
http://%20www.microsoft.com/
which doesn't work...
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>",$con tent1);
$content1= preg_replace("/(\s)(\www\. .*?(\s|$)) /si","\\1< a href=\"http://\\2\">\\2</a>",$content 1);
$content1= preg_replace("/(\http:\/\/.*?(\s|$))/si","<a href=\"\\1\">\\1</a>",$con
$content1= preg_replace("/(\s)(\www\.
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 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
Batalf
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect, thanks so much
Glad I could help
<?
$string = "http://www.webserveraddress.com";
$string = "<a href=\"$string\">$string</
?>