note 49755 deleted from function.preg-replace by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jun 2006 13:23:58 +0000 |
| Subject: | note 49755 deleted from function.preg-replace by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114334@lists.php.net to get a copy of this message | ||
Note Submitter: info at atjeff dot co dot nz
----
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 :)