Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I made a custom bbcode parser function and added it on my helper

if ( !function_exists('bbcode_parser'))
{
    function bbcode_parser($str) 
    {
        $patterns = array(
            '#\[b\](.*?)\[/b\]#is',
            '#\[img\](.*?)\[/img\]#is',
            '#\[url\](.*?)\[/url\]#is',
            '#\[url=(.*?)\](.*?)\[/url\]#is'
        );

        $replacements = array(
            '<strong>$1</strong>',
            '<img src="$1" />',
            '<a href="$1">$1</a>',
            '<a href="$1">$2</a>',
        );

        $str = preg_replace($patterns, $replacements, $str);
        return $str;
    }
}

It's good and works like I want it to but my question is how to apply a function on each replacement value.

fe. for the url that doesn't have inner data, i want to replace with the website title of the url

or validate the url if it has http://

i would also like to check the size of the image, if it's too large, i want to resize it when printed by adding a "width" attribute and then just add an tag to the full size image.

is this possible? if so, how to do this?

share|improve this question

1 Answer

You can implement this with preg_replace_callback() http://www.php.net/manual/en/function.preg-replace-callback.php

Only the matched elements will be passed to the callback, so you would be able to create the callbacks you need or apply a standard replace for the normal regex patterns.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.