Regular expression help in PHP

Go To StackoverFlow.com

2

I have a block of text where I need yo find bits of text like: {slider:1} {video-alt:10}

I have this bit of code

$regex = '/{[ ]*(slider)|(slider-alt)|(video)[ ]*:[0-9]+[ ]*}/';
        $matches = array();
        preg_match_all( $regex, $row->content, $matches );

But the array returned is all messed up...

Array output:

Array ( [0] => {slider [1] => {slider [2] => video:2} )
Array ( [0] => slider [1] => slider [2] => )
Array ( [0] => [1] => [2] => )
Array ( [0] => [1] => [2] => video )

For the input:

{slider:6}
{slider-alt:2}
{video:2}

Any help?

2012-04-04 07:16
by jribeiro


2

Your regexp is messy.

$regex = '/{ *(slider|slider-alt|video) *:\d+ *}/';
$matches = array();
preg_match_all( $regex, $row->content, $matches );
2012-04-04 07:20
by meze
Thanks but this still does do it... I've update the anwser with the result of your changes - jribeiro 2012-04-04 07:22
Where is your updated answer? var_dump($matches[0]); gives array(3) { [0]=> string(10) "{slider:6}" [1]=> string(14) "{slider-alt:2}" [2]=> string(9) "{video:2}" }meze 2012-04-04 07:23
Sorry It's there now ; - jribeiro 2012-04-04 07:24
Isn't the first array what you need? ; - meze 2012-04-04 07:24
You're absolutely right! Sorr - jribeiro 2012-04-04 07:25
Ads