note 10443 added to ereg_replace
| From: | ckrempea at sfu dot ca | Date: | Wed, 03 Jan 2001 22:41:07 +0000 |
| Subject: | note 10443 added to ereg_replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-9359@lists.php.net to get a copy of this message | ||
I've seen various notes added for turning URLs (in text) into hyperlinks.
Many of them seem to put [[::space:]] before and after to find it. But this will NOT work if the
link (or whatever you are looking for) starts at the **very beginning** of the string; or the **very
end** of the string.
What is needed is to look for the (^|[[:space:]]) at the beginning; and the ([[:space:]]|$) at the
end.
For example here are a few types of URLs (http, https, and ftp) that are converted...
$content = ereg_replace("(^|[[:space:]])(http://[^[:space:]]{1,})([[:space:]]|$)",
"\\1<a href=\"\\2\">\\2</a>\\3", $content);
$content = ereg_replace("(^|[[:space:]])(https://[^[:space:]]{1,})([[:space:]]|$)",
"\\1<a href=\"\\2\">\\2</a>\\3", $content);
$content = ereg_replace("(^|[[:space:]])(ftp://[^[:space:]]{1,})([[:space:]]|$)",
"\\1<a href=\"\\2\">\\2</a>\\3", $content);
And, since I didn't notice it mentioned above,... you can create smileys (for a bulletin board
or whatever) with...
$content = ereg_replace('(^|[[:space:]]):-\)([[:space:]]|$)', "\\1<img
src=\"image/smiley-smile.gif\">\\2", $content);
$content = ereg_replace('(^|[[:space:]]):-\(([[:space:]]|$)', "\\1<img
src=\"image/smiley-frown.gif\">\\2", $content);
(For converting :-) and :-( .. You'll have to set the image sources -- the
src'
attribute in the img' tag -- to whatever you smiley image files are.)
Of course you can use this technique for whatever you want. (Not just hyperlinks and smileys.) (For
example, like censoring out swearing, for a kid-safe site,... or whatever). Just use...
(^|[[:space:]])
... for the beginning. And...
([[:space:]]|$)
...for the end
-----
Charles Iliya Krempeaux
http://www.php.net/manual/function.ereg-replace.php