tags:

views:

39

answers:

1

Hello all,

I have a lot to learn as far as regular expressions are concerned.

I have an associative array like so:

array(
    "label"=>"Special",
    "title"=>"Category",
    "onclick"=>"dosomething()",
    "options"=>array(
        "one"=>"something"
    )
)

I am trying to use preg_match_all on the array like so:

$match="on*";
foreach ($value as $param=>$text) {
    if (preg_match_all("/".$match."/",$param,$matches)) {
        $return.=" ".$param."='".$text."'";
    }
}
return $return;

My problem is $return ends up looking like this:

 options='Array' onclick='dosomething()'

Obviously, my regex is wrong. on* is not sufficient - It is matching 'options' as well. :(

Can anyone tell me what would be correct regex to use?

+1  A: 

You're testing for "on" anywhere in the string. You'll need to anchor the "on" to the front of the string with this:

/^on/

Cheers.

infamouse
i knew it would be a simple correction. waiting on timeout to accept answer.
Actually `on*` tests for *'o' followed by zero or more 'n'* anywhere in the string. In regex `*` is not a simple wildcard character.
MooGoo
use . as a simple wildcard character. use .* to match any number of any characters.
Razor Storm
From the return value you'll notice that "on" is in the middle of the string. I don't think this will match it.
slebetman