Regex

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Regex

Post by Darkzaelus »

Hello, i'm at my wits end with this piece of code.
I'm rewriting my template engine to deal with multiple parts of an if statement, eg:

Code: Select all

{if $login_disabled&&$login_disabled}
I have a preg_replace_callback function that works fine, it gets the

Code: Select all

$login_disabled&&$login_disabled
part out of the statement.

Only problem now is that whenever I do a regex statement, match or replace, it only captures the first letter after the dollar sign:

Code: Select all

 
preg_match('/\\$([a-z0-9]+?)/is', $matches[1], $match);
 
yields:

Code: Select all

 
Array
(
    [0] => $l
    [1] => l
)
 
What i'm trying to do is get it into the form of:

Code: Select all

 
<?php if($this->PV_vars['login_disabled']): ?>
 
with this code:

Code: Select all

 
preg_replace('/\\$([a-z0-9]+?)/is', '\$this->PV_vars(\'$1\')', $matches[1]);
 
Instead what comes out is:

Code: Select all

 
<?php if(!$this->PV_vars('l')ogin_disabled): ?>
 
Is there something stupid i'm doing?


Cheers,

Darkzaelus
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Regex

Post by mintedjo »

Uhmmmm

Code: Select all

[a-z0-9]+?
is a lazy match, meaning it will match the smallest string that matches and fits in with the surrounding regex (if its possible).
In this case the smallest string is the first alphabetic character - "l".

Try replacing

Code: Select all

[a-z0-9]+?
with

Code: Select all

[a-z0-9_]+
?
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: Regex

Post by Darkzaelus »

hah, cheers for that.

I feel a bit of a pillock now lol.

Darkzaelus.
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Regex

Post by mintedjo »

Darkzaelus wrote:I feel a bit of a pillock now
Because you are...


j/k
Post Reply