Posted By

aamirrajpoot on 09/14/11


Tagged

regular Expression


Versions (?)

URLs in Text, and Parsing them into Hyperlinked HTML urls


 / Published in: PHP
 

URL: http://www.webmasterworld.com/php/3268855.htm

Try something like this.

$pattern = "/http:\/\/(www.)?([^.]+.[^.\s]+.?[^.\s]*)/i"; $replace = "http://\1\2"; $string = [url=http://us2.php.net/preg-replace]preg_replace/url; echo $string;

Where $string is the text. Just a couple notes about this pattern: 1) The URI MUST have "http://" before it 2) It works for URIs formatted in the following ways

http://www.example.foo http://www.example.foo.foo #having two http://example.foo http://example.foo.foo #having two http://foo.exmaple.foo #pretty much the same as above

  1. Try something like this.
  2.  
  3. $pattern = "/http:\/\/(www\.)?([^.]+\.[^.\s]+\.?[^.\s]*)/i";
  4. $replace = "<a href='http://\\1\\2'>http://\\1\\2</a>";
  5. $string = [url=http://us2.php.net/preg-replace]preg_replace[/url]($pattern,$replace,$string);
  6. echo $string;
  7.  
  8. Where $string is the text. Just a couple notes about this pattern:
  9. 1) The URI MUST have "http://" before it
  10. 2) It works for URIs formatted in the following ways
  11. >>>http://www.example.foo
  12. >>>http://www.example.foo.foo #having two
  13. >>>http://example.foo
  14. >>>http://example.foo.foo #having two
  15. >>>http://foo.exmaple.foo #pretty much the same as above

Report this snippet  

You need to login to post a comment.