RE: preg_replace and regular expressions.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx] 
Sent: Sunday, April 15, 2007 12:22 AM
To: php-general@xxxxxxxxxxxxx
Subject:  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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux