Page 1 of 1

Confused about matches

Posted: Tue Mar 03, 2009 1:22 pm
by jon23d
I have a string in which I need to replace several tokens.

The tokens are {{small-images}}, {{medium-images}}, {{images}}, {{xl-images}}, and {{xxl-images}}

I want to take the first one found, and then replace all the others with that.

Then I want to replace all the tokens with a given string.

But I can't seem to get past the first part...

As a test, I'm running:

Code: Select all

 
$pattern = "/{{([a-zA-Z]-)?images}}/";
$string = "this is a test {{small-images}}.  Yep{{medium-images}} and {{images}} and {{xl-images}} and {{xxl-images}}.";
 
if (preg_match($pattern, $string, $matches)) {
    echo preg_match($pattern, $string) . " matches found:<br />";
    print_r($matches);
}
 
For whatever reason, only 1 match is being found, and it is just {{images}}, not {{small-images}}...

Re: Confused about matches

Posted: Tue Mar 03, 2009 1:30 pm
by pickle
You're only matching one occurrence of a-zA-Z, so the rest of the word isn't found.

This pattern works:

Code: Select all

/{{[a-zA-Z]*-images}}/
If you know exactly what the strings are, use str_replace() - it's much more efficient.

Re: Confused about matches

Posted: Tue Mar 03, 2009 1:39 pm
by jon23d
I can use str_replace after determining what the first token is, so I think I have to use regex first.

I made a slight change to yours, and now it works perfectly (though I still only get 1 match, but I guess that doesn't matter):

Code: Select all

/{{[a-zA-Z-]*images}}/
Thank you!

Re: Confused about matches

Posted: Tue Mar 03, 2009 2:09 pm
by pickle
If you want multiple matches, use preg_match_all()

Maybe I'm not understanding your problem 100%. Why you you need to determine what the first token is? Even then - strpos() 6 times is probably more efficient.

Regular expressions have tons of overhead & should only be used when you don't know exact content of strings.

Re: Confused about matches

Posted: Wed Mar 04, 2009 10:09 am
by prometheuzz
jon23d wrote:...

For whatever reason, only 1 match is being found, and it is just {{images}}, not {{small-images}}...
This is what your regex really means:

Code: Select all

{{            // match '{{'
(             // open group 1
  [a-zA-Z]    //   match a single character in the range a-z or A-Z
  -           //   match a hyphen
)             // close group 1
?             // group 1 is optional
images        // match 'images'
}}            // match '}}'
So, as you can see, the substring "{{images}}" only matches it.

You probably meant to do something like this:

Code: Select all

$pattern = "/{{(?:[a-zA-Z]*-)?images}}/";
$string = "this is a test {{small-images}}.  Yep{{medium-images}} and {{images}} and {{xl-images}} and {{xxl-images}}.";
 
if (preg_match_all($pattern, $string, $matches)) {
    echo preg_match($pattern, $string) . " matches found:<br />";
    print_r($matches);
}
And replacing all the '{{ ... }}' with the first occurrence of '{{ ... }}' ('{{small-images}}' in this case), can be done like this:

Code: Select all

$pattern = "/{{(?:[a-zA-Z]*-)?images}}/";
$string = "this is a test {{small-images}}.  Yep{{medium-images}} and {{images}} and {{xl-images}} and {{xxl-images}}.";
 
if (preg_match($pattern, $string, $matches)) {
    echo preg_replace($pattern, $matches[0], $string);
}