tags:

views:

46

answers:

3

Hay i have a string like

[gallery GALLERYNAME] [gallery GALLERYNAME]

the GALLERYNAME indicates a name of a gallery

What would a regular expression looks like to match this string?

+4  A: 
\[gallery ([^\]]+)\]
reko_t
See edit in my post.
dotty
The answer is still the same, though.
reko_t
+1  A: 

To match "GALLERYNAME" :

^\[gallery\s([A-Z]+)\]$
Spilarix
+3  A: 
<?php
    preg_match( "@\[gallery ([^\]]+)\]@", "foo [gallery GALLERYNAME] bar", $match );
    var_dump( $match );
    // $match[ 1 ] should contain "GALLERYNAME"
?>

As an alternate:

<?php
    preg_match_all( "@\[gallery ([^\]]+)\]@m", "
        foo [gallery GALLERYNAME1] bar
        foo [gallery GALLERYNAME2] bar
        foo [gallery GALLERYNAME3] bar
        ", $match );
    var_dump( $match );
    // $match[ 1 ] should contain an array containing the desired matches
?>
Salman A
What happens if the string [gallery GALLARY] comes up twice?
dotty
In that case use preg_match_all(). I am not exactly sure what $match will contain in that case; but a var_dump will make things more clear.
Salman A
I've edited the post now.
Salman A