Re: Reg-ex help
| From: | Jochem Maas | Date: | Thu, 05 Feb 2009 10:22:58 +0000 |
| Subject: | Re: Reg-ex help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287689@lists.php.net to get a copy of this message | ||
Craige Leeder schreef:
> Hey guys,
>
> I'm trying to write a regular expression to match a tag for my
> frameworks template engine. I seem to be having some trouble. The
> expression should match:
>
> {:seg 'segname':}
> {:seg 'segname' cache:}
>
> What I have is...
>
> $fSegRegEx = "#\{:seg \'[a-z0-9\-\_]{3,}\'( cache)?:\}#i";
hth:
<?php
$fSegRegEx = "#(\{:seg )\'([a-z0-9\-\_]{3,})\'((?: cache)?:\})#i";
$str = <<<DATA
<html>
<body>
<h1>{:seg 'title':}</h1>
<h1>{:seg 'title' cache:}</h1>
</body>
</html>
DATA;
preg_match_all($fSegRegEx, $str, $faSegMatches, PREG_SET_ORDER);
print_r($faSegMatches);
?>
OUTPUTS:
Array
(
[0] => Array
(
[0] => {:seg 'title':}
[1] => {:seg
[2] => title
[3] => :}
)
[1] => Array
(
[0] => {:seg 'title' cache:}
[1] => {:seg
[2] => title
[3] => cache:}
)
)
>
> Which when run against my test data, seems to match:
>
> "{:seg 'segname' cache:}"
> " cache"
>
>
>
>
>
> For arguments sake, I'll provide the whole code snippet...
>
> preg_match($fSegRegEx, $this->msOpCont, $faSegMatches);
>
> /* faSegMatches ==
>
> * array
>
> * 0 => '{:seg 'users_online' cache:}'//
>
> * 1 => ' cache'//
>
> */
>
> while ( $fMatch = current($faSegMatches) ) {
>
> /*
> * Expected:
>
> * $fMatch[0] = "{:seg "
>
> * $fMatch[1] = Segment Name
>
> *
> * $fMatch[2] = " cache:}"
>
> * or
>
> * $fMatch[2] = " :}"
>
> */
>
> $faMatch = explode("'", $fMatch);
>
> //...
>
> //...
>
> next($faSegMatches);
>
> }// End While
>
>
> Thanks guys. This has been bugging me for a couple days.
> - Craige
>
>