Re: Regex help please

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

 



haliphax wrote:
> On Fri, Mar 27, 2009 at 9:40 AM, Shawn McKenzie <nospam@xxxxxxxxxxxxx> wrote:
>> I'm normally OK with regex, especially if I fiddle with it long enough,
>> however I have fiddled with this one so long that I'm either totally
>> missing it or it's something simple.  Does it have anything to do with
>> the backref, or the fact that the value of the backref has a $?  I have:
>>
>> $out = '
>> {$sites}
>> <tr>
>>        <td>
>>        {Site.id}
>>        </td>
>> </tr>
>> {/$sites}';
>>
>> And I want to capture the first {$tag}, everything in between and the
>> last {$/tag}.  I have tried several things and here is my current regex
>> that looks like it should work, but doesn't:
>>
>> preg_match_all('|{\$([^}]+)}(.+)({/\1})|Us', $out, $matches);
> 
> Shawn,
> 
> First thing I see--your first capture group doesn't include the $, and
> so your final capture group will always fail given your current $out
> (because it's looking for {/sites} instead of {/$sites}). Also, your
> {} are outside of your capture group in \1, but inside in \3. Here's
> what I came up with:
> 
> $out = '
> {$sites}
> <tr>
>        <td>
>        {Site.id}
>        </td>
> </tr>
> {/$sites}';
> $matches = array();
> preg_match_all('#{(\$[^}]+)}(.*?){(/\1)}#s', $out, $matches);
> print_r($matches);
> 
> Produces this:
> 
> Array
> (
>     [0] => Array
>         (
>             [0] => {$sites}
> <tr>
>        <td>
>        {Site.id}
>        </td>
> </tr>
> {/$sites}
>         )
> 
>     [1] => Array
>         (
>             [0] => $sites
>         )
> 
>     [2] => Array
>         (
>             [0] =>
> <tr>
>        <td>
>        {Site.id}
>        </td>
> </tr>
> 
>         )
> 
>     [3] => Array
>         (
>             [0] => /$sites
>         )
> 
> )
> 
> Keep in mind, I had to view the page source in order to see the HTML
> tags, but it showed me everything I expected to see.
> 
> HTH,
> 

Yes, thank you.  I was fiddling before I got your post and I came up
with roughly the same.

preg_match_all('|{(\$[^}]+)}(.+){(/\1)}|Us', $out, $matches);

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
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