Connecting Tech Pros Worldwide Help | Site Map

Question on PHP regular expressions

sberry
Guest
 
Posts: n/a
#1: Nov 8 '07
In PHP I have many more options for functions that do regular
expression than I did in Perl. My question is what is the best one to
use for doing the following:

Give the following:
{Somestring.somestring.somestring} blah blah {somestring} value here
not used {more.text.here}

I want an array like the following:
[0] =Somestring.somestring.somestring
[1] =somestring
[2] =more.text.here

So basically I just want to capture the contents (alpha-numeric +
periods) inside of curly braces.

Thanks for any help.

Carl
Guest
 
Posts: n/a
#2: Nov 8 '07

re: Question on PHP regular expressions


sberry <sean@buildingonline.comwrites:
Quote:
In PHP I have many more options for functions that do regular
expression than I did in Perl. My question is what is the best one to
use for doing the following:
>
Give the following:
{Somestring.somestring.somestring} blah blah {somestring} value here
not used {more.text.here}
>
I want an array like the following:
[0] =Somestring.somestring.somestring
[1] =somestring
[2] =more.text.here
>
So basically I just want to capture the contents (alpha-numeric +
periods) inside of curly braces.
>
Thanks for any help.
This should get you started:

$string = "{Somestring.somestring.somestring} blah blah
{somestring} value here not used {more.text.here}";
$pattern = '/\{.+?\}/';
preg_match_all($pattern, $string, $matches);
print_r($matches);

Hope that helps,
sberry
Guest
 
Posts: n/a
#3: Nov 8 '07

re: Question on PHP regular expressions


On Nov 8, 11:17 am, Carl <c.gro...@gmail.comwrote:
Quote:
sberry <s...@buildingonline.comwrites:
Quote:
In PHP I have many more options for functions that do regular
expression than I did in Perl. My question is what is the best one to
use for doing the following:
>
Quote:
Give the following:
{Somestring.somestring.somestring} blah blah {somestring} value here
not used {more.text.here}
>
Quote:
I want an array like the following:
[0] =Somestring.somestring.somestring
[1] =somestring
[2] =more.text.here
>
Quote:
So basically I just want to capture the contents (alpha-numeric +
periods) inside of curly braces.
>
Quote:
Thanks for any help.
>
This should get you started:
>
$string = "{Somestring.somestring.somestring} blah blah
{somestring} value here not used {more.text.here}";
$pattern = '/\{.+?\}/';
preg_match_all($pattern, $string, $matches);
print_r($matches);
>
Hope that helps,
Thanks. That is exactly what I wanted to do. I just added the
PREG_OFFSET_CAPTURE so I could also track the string offsets of the
matches.

Closed Thread