RE: [PHP] preg_replace and regular expressions.
| From: | Buesching, Logan J | Date: | Sun, 15 Apr 2007 16:04:58 +0000 |
| Subject: | RE: [PHP] preg_replace and regular expressions. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252967@lists.php.net to get a copy of this message | ||
In your regex, you have a greedy matcher, i.e. ".*" will match as much
as it can to satisfy its condition. I believe you can do ".*?" and it
will work, as ".*?" will match as little as it can to be satisfied.
-Logan
-----Original Message-----
From: Travis Moore [mailto:trabus2005@gmail.com]
Sent: Sunday, April 15, 2007 12:22 AM
To: php-general@lists.php.net
Subject: [PHP] preg_replace and regular expressions.
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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php