views:

72

answers:

2

Hi Guys ive tried to search for this answer all morning, but with no luck, all i want to do is match [slideshow or [gallery with the included [ bracket..

code as follows.

$gallery = get_post_meta($post->ID, 'gallery', true);


if (preg_match("|^/[slideshow", $gallery)) {
    echo "Slideshow was forund";
} else if (preg_match("|^/[nggallery", $gallery)) {
   echo "Gallery was found";
} else {
   echo "No Match found - No Meta Data available"; 
}

The regular expression ive used, i though would work like this. search the start of the string, and using / would escape the [ from being used as part of the regular expression and be part of the search,

regular expressions is just not my thing.... although the more reading i do, it becomes a little more clearer..

thanks

+7  A: 

The escape character is \ not /. Furthermore, you need to end the regex with the same delimiter as at the start of the regex. So your code will need to be something like this:

preg_match("|^\[slideshow|", $gallery)
Residuum
thanks a lot, still a lot to read me thinks.
Marty
Although both answers are correct, this one does a better job of explaining what needed to be changed and why.
Jonathan Fingland
+2  A: 
if (preg_match("/^\[slideshow/", $gallery)) {
    echo "Slideshow was forund";
} else if (preg_match("/^\[nggallery/", $gallery)) {
   echo "Gallery was found";
} else {
   echo "No Match found - No Meta Data available"; 
}

Changes made:

The [ needs to be escaped as its a metachar, the escape char to be used is \. Also preg_match expects its first argument(regex) to be delimited between suitable char. So you can do:

preg_match("/^\[slideshow/", $gallery)

or

preg_match("|^\[slideshow|", $gallery)
codaddict
have to say, thanks, works a charm, thanks for your help...
Marty
Always a pleasure :)
codaddict