[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

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
0
jmingo
Asked:
jmingo
  • 9
  • 8
1 Solution
 
BatalfCommented:
Try

<?

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

?>

0
 
jmingoAuthor Commented:
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
0
 
BatalfCommented:
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

?>

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jmingoAuthor Commented:
thanks i'll try that.... what if they put in www.cnn.com instead of http://www.whatever.com ???
0
 
BatalfCommented:
Doesn't matter. It searches for substrings that starts with "http://"
0
 
jmingoAuthor Commented:
if the string contains these

http://www.cnn.com 

www.microsoft.com

only the cnn links.
0
 
BatalfCommented:
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
0
 
jmingoAuthor Commented:
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);
0
 
BatalfCommented:
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);
0
 
jmingoAuthor Commented:
www.microsoft.com opens up a browser but the link is this

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

which doesn't work...
0
 
BatalfCommented:
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);
0
 
jmingoAuthor Commented:
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.
0
 
BatalfCommented:
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
0
 
jmingoAuthor Commented:
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
0
 
BatalfCommented:
This

$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>\\3",$content1);

should get rid of the %20 problem in Firefox.
0
 
jmingoAuthor Commented:
perfect, thanks so much
0
 
BatalfCommented:
Glad I could help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now