/ Published in: PHP
Try something like this.
$pattern = "/http:\/\/(www\.)?([^.]+\.[^.\s]+\.?[^.\s]*)/i";
$replace = "<a href='http://\\1\\2'>http://\\1\\2</a>";
$string = [url=http://us2.php.net/preg-replace]preg_replace[/url]($pattern,$replace,$string);
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
$pattern = "/http:\/\/(www\.)?([^.]+\.[^.\s]+\.?[^.\s]*)/i";
$replace = "<a href='http://\\1\\2'>http://\\1\\2</a>";
$string = [url=http://us2.php.net/preg-replace]preg_replace[/url]($pattern,$replace,$string);
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Try something like this. $pattern = "/http:\/\/(www\.)?([^.]+\.[^.\s]+\.?[^.\s]*)/i"; $replace = "<a href='http://\\1\\2'>http://\\1\\2</a>"; $string = [url=http://us2.php.net/preg-replace]preg_replace[/url]($pattern,$replace,$string); 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
URL: http://www.webmasterworld.com/php/3268855.htm