preg_replace and regular expressions.
| From: | Travis Moore | Date: | Sun, 15 Apr 2007 04:22:18 +0000 |
| Subject: | preg_replace and regular expressions. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-252958@lists.php.net to get a copy of this message | ||
Okay, so what I have is a BB code type of thing for a CMS, which I for obvious reasons can't allow HTML.
Here's the snippet of my function:
********
function bbCode($str)
{
$db = new _Mysql;
$strOld = $str;
$strNew = $str;
$getRegexs = $db->query("SELECT
regex,replace,search FROM _bb_codes");
while ($getRegex = mysql_fetch_assoc($getRegexs))
{
$search = base64_decode($getRegex['search']);
$regex = base64_decode($getRegex['regex']);
$replace = base64_decode($getRegex['replace']);
if (preg_match($search,$strNew) == 1)
{
for ($i = 1; $i < 20; $i++)
{
$strNew = $strOld;
$strNew = preg_replace($regex,$replace,$strNew);
if ($strNew == $strOld)
{
break;
}
else
{
$strOld = $strNew;
}
}
}
}
$return = $strNew;
return $return;
}
**********
But, for something like this:
[quote][quote]Quote #2[/quote]Quote #1[/quote]No quote.
I'll get:
<div class="quoteContainer">
[quote]Quote #2[/quote]Quote #1</div>
No quote.
Despite being in the loop.
Regex is: /\[quote\]((.*|\n)*)\[\/quote\]/
Replace is: <div class="messageQuote">$1</div>
Both are stored base64 encoded in a database.
Any help / suggestions much appreciated.