note 49755 added to function.preg-replace
| From: | info at atjeff dot co dot nz | Date: | Tue, 08 Feb 2005 00:47:51 +0000 |
| Subject: | note 49755 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84640@lists.php.net to get a copy of this message | ||
ive never used regex expressions till now and had loads of difficulty trying to convert a [url]link
here[/url] into an href for use with posting messages on a forum, heres what i manage to come up
with:
$patterns = array(
"/\[link\](.*?)\[\/link\]/",
"/\[url\](.*?)\[\/url\]/",
"/\[img\](.*?)\[\/img\]/",
"/\[b\](.*?)\[\/b\]/",
"/\[u\](.*?)\[\/u\]/",
"/\[i\](.*?)\[\/i\]/"
);
$replacements = array(
"<a href=\"\\1\">\\1</a>",
"<a href=\"\\1\">\\1</a>",
"<img src=\"\\1\">",
"<b>\\1</b>",
"<u>\\1</u>",
"<i>\\1</i>"
);
$newText = preg_replace($patterns,$replacements, $text);
at first it would collect ALL the tags into one link/bold/whatever, until i added the "?"
i still dont fully understand it... but it works :)
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49755
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49755&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49755&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49755&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49755&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49755&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49755&report=yes
Search -- http://master.php.net/manage/user-notes.php