note 57737 added to function.preg-replace
| From: | NickBullock-SafireX at osu1 dot php dot net | Date: | Wed, 12 Oct 2005 20:46:29 +0000 |
| Subject: | note 57737 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96917@lists.php.net to get a copy of this message | ||
Here's a quick preg_replace function.
It will scan an html or plain text string stored in a variable and change all web urls to html
hyperlinks. These will popup in a new window. The ' target="popup" ' can easily
be removed to make links appear in the current window.
Useful when building forums and pulling messages or posts from a database or text file. Can also be
used with <? ob_start(); ?> and <? $html = ob_get_clean(); ?> to process output buffer.
For example any text matching:
www.safirex.co.uk or http://www.safirex.co.uk etc.
will be replaced with:
<a href="http://www.safirex.co.uk"
target="popup">www.safirex.co.uk</a>
-----------------------------------------------------------
<?
function linkScan($string)
{
$pattern = "/(http:\/\/)?www([\w\d\.\-]+)(\/[\w\d\.\-_]*)?/i";
$replace = "<a href=\\"http://www$2$3\\"
target\\"popup\\">$0</a>";
return( preg_replace($pattern,$replace,$string) );
}
?>
-----------------------------------------------------------
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57737
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57737&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57737&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57737&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57737&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57737&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57737&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57737&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57737&report=yes
Search -- http://master.php.net/manage/user-notes.php