Hello all, Let me just get straight to the mix. I have a string: {this is} a {test}. I want to capture both "this is" and "test" in an array using preg_match_all (or another function if something else should be applied here). I simply can't figure out how to do it. Any help on that would be great.
Thanks that worked great (rep given). Now I ran into another problem that I know i'm doing something stupid on I just can't quite figure it out. When I try to echo($matches[0]); it returns the term "Array" rather than actually what is inside of it. What am I doing wrong here? I have tried both print() and echo().
It should be: echo $matches[1][0]; // this is echo $matches[1][1]; // test PHP: If you do a: print_r($matches); PHP: ... you'll see everything clearer.
Curious, I've seen you use that many times. What's the difference between using ~ and using / as the start/end of a match expression? (I've always used / because I saw that on PHP's website)
It's the same basically. But I prefer ~, so that I don't have to escape all slashes that I want to match. Let's take this pattern as example. It checks if a string begins with http:// '~^http://~' PHP: If I use the slash as delimiter, I'd have to escape all slashes: '/^http:\/\//' PHP: ... which is ugly. So I prefer ~. Now I'm using it always 'cause I got used to it. But other than that, it's the same... You can also use @, !, |, etc... as delimiter.
Ah, I see. Most of the time I don't like using any delimiters so I just stick with ereg/eregi (I don't see any differences except not being able to put modifiers at the end of it, which the only one I ever use is 'i' and eregi already does case-insensitive).
The preg_* functions are about 6 times faster than the ereg_* functions. Now for small strings like URLs, it doesn't matter, but I generally prefer the preg_* functions.
I never realized the preg_* functions were faster than ereg_*s. I originally decided to use them just because they seemed more powerfull and configurable.