0

I have a string that looks something like this:

{{imagename.jpg|left|The caption for this image. Includes a variety of chars}}
<p>Some text, lots of paragraphs here.</p>
{{anotherimage.jpg|right|Another caption.}}

What I'm trying to do is to parse out the {{}} bits then pass them through a function. What I have so far is:

function template_function($matches) {
    print_r($matches);
}

function parse_images($string) {
    $string = preg_replace_callback('!\{\{([^}])\}\}!', 'template_function', $string);
    return $string;
}

Can someone give me a hand with the regex so that I end up with the matches being run through print_r?

2 Answers 2

1
function template_function($matches) {
    print_r($matches[1]);
}

function parse_images($string) {
    $string = preg_replace_callback('/\{\{([^}]*)\}\}/', 'template_function', $string);
    return $string;
}

Have also modified print_r($matches[1]); so that the the actual match is printed.

Sign up to request clarification or add additional context in comments.

1 Comment

So, I missed the bleeding *! I knew that I must be almost there! This is why Stack Overflow is so awesome - just getting someone else to glance at your code is very very useful.
1

You missed the * (or, perhaps the +) quantifier. Your original expression would only match a single, non-} character.

$string = preg_replace_callback('!\{\{([^}]*)\}\}!', 'template_function', $string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.