views:

59

answers:

4

We use custom bbcode in our news posts

[newsImage]imageName.jpg[/newsImage]

And i'd like to use preg_match to get the imageName.jpg from between those tags. The whole post is stored in a variable called $newsPost.

I'm new to regex and I just can't figure out the right expression to use in preg_match to get what I want.

Any help is appreciated. Also, do any of you know a good resource for learning what each of the characters in regex do?

+2  A: 
preg_match_all('/\[newsImage\]([^\[]+)\[\/newsImage\]/i', $newsPost, $images);

The variable $images should then contain your list of matches.

http://www.php.net/manual/en/regexp.introduction.php

Rob
Since I only wanna match the first instance, I'm only using preg_match, and that tells me $images is an array. So i tried $images[1] and $images[0] but I get 0 results for both.
scatteredbomb
Fixed the last bracket! `preg_match` *should* return your image name as $images[1] or `false` if nothing is matched.
Rob
Thanks. I'm not entirely sure why, but that still didn't work for me. I was able to get it working with this /\[newsImage](.*?)\[\/newsImage\]/is
scatteredbomb
Ah, figured it out. Just needed to include that last / for [/newsImage] and cancel it out [\/newsImage]. Thanks so much for your help.
scatteredbomb
Sorry about that! Fixed it for posterity.
Rob
A: 

This is not exactly what you asked for, but you can replace your [newsImage] tags with tags using the following code, its not perfect as it will fall down if you have an empty tag e.g. [newsImage][/newsImage]

function process_image_code($text) {
        //regex looks for [newsImage]sometext[/newsImage]
        $urlReg ="/((?:\[newsImage]{1}){1}.{1,}?(?:\[\/newsImage]){1})/i";
        $pregResults = preg_split ($urlReg , $text, -1, PREG_SPLIT_DELIM_CAPTURE);
        $output = "";

        //loop array to build the output string
        for($i = 0; $i < sizeof($pregResults); $i++) {

            //if the array item has a regex match process the result
            if(preg_match($urlReg, $pregResults[$i]) ) {
                $pregResults[$i] = preg_replace ("/(?:\[\/newsImage]){1}/i","\" alt=\"Image\" border=\"0\" />",$pregResults[$i] ,1);

                // find if it has a http:// at the start of the image url
                if(preg_match("/(?:\[newsImage]http:\/\/?){1}/i",$pregResults[$i])) {
                    $pregResults[$i] = preg_replace ("/(?:\[newsImage]?){1}/i","<img src=\"",$pregResults[$i] ,1);
                }else {
                    $pregResults[$i] = preg_replace ("/(?:\[newsImage]?){1}/i","<img src=\"http://",$pregResults[$i] ,1);
                }

                $output .= $pregResults[$i];
            }else {
                $output .= $pregResults[$i];
            }
        }

        return $output;
    }
Re0sless
+1  A: 

To answer your second question: A very good regex tutorial is regular-expressions.info.

Among other things, it also contains a regular expression syntax reference.

Since different regex flavors use a different syntax, you'll also want to look at the regex flavor comparison page.

Tim Pietzcker
+2  A: 

As Rob said but escaping last ]

preg_match('/\[newsImage\]([^\[]+)\[newsImage\]/i', $newsPost, $images);

$images[1] will contain the name of image file.

M42