In PHP, how would you detect a URL contained in a string of text and format it to display as a friendly URL?
-
For instance, if someone posted something like "This is a pretty cool web site www.google.com/?var1=something&var2=somethingelse&var3=stuff" How would you format it like: "This is a pretty cool web site <a href="http://www.mysite.com/?urlclicked=http://www.google.com/?var1=something&var2=somethingelse&var3=stuff">www.google.com/?var1=something...</a>" The link would first take you to an internal page to record that the link was clicked and then open a new browser window with the destination.
-
Answer:
Use preg_replace() with a regular expression to detect the url and replace it with a link leading to it. You can find a number of more or less permissive url matching regexes online. Here's one example: $text = preg_replace('#(https?\://)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?#', '<a href="$1">$1</a>', $text);
Anton Stroganov at Quora Visit the source
Other answers
$content = preg_replace('#(https?\://)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?#', '<a href="$1">$1</a>', $content); if((strpos($content,'http://')==false)&&(strpos(%24content,%22www%22)==true)) $content = preg_replace('#www\.[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?#', '<a href="$1">$1</a>', $content); you can test whether it works by using regex tester : http://ccmpp.com/Regex/regex%20tester.htm
Kemp Mk
you can use preg_match_all: if ( preg_match_all("/\b(http\:\/\/[^\s]+)\b/", $text, $links, PREG_PATTERN_ORDER) ) { foreach ( $links[1] as $lnk ) { $text = str_replace($lnk, "<a target='_blank' href='http://www.mysite.com/?urlclicked=".urlencode($lnk)."'>$lnk</a>", $text); } } or you can use preg_replace with the /e modifier $text = preg_replace("/\b(http\:\/\/[^\s]+)\b/e", "'<a href=\"http://www.mysite.com/?urlclicked='.urlencode('\\1').'\">\\1</a>'", $text); (i didn't test the preg_replace option so i might have gotten the sytax wrong. take a look at http://us.php.net/manual/en/function.preg-replace.php for more info)
Travis Kuhl
Related Q & A:
- How can I echo characters before and after a string?Best solution by stackoverflow.com
- How do I retrieve a URL in Java?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How do you hide a url on 2.0 myspace?Best solution by Yahoo! Answers
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.