1

With security and also a deprecated function; what would be the easiest and most secure way to call a function in a find and replace?

There are four find and replace modules which can be inserted within content [album][/album], [img][/img], [youtube][/youtube], or [vimeo][/vimeo].

Using the function I put together so far Images, YouTube and Vimeo were a no brainer. The Album no so much. I would like to call a function based on parameters that are passed.

I tried altering this function into a preg_replace_callback and that just mocks up everything. Is there any alternatives?

function FormatModules($text) {
    $find = array(
        '~\[album\](.+?)\[/album\]~s',
        '~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
        '~\[youtube\](.+?)\[/youtube\]~s',
        '~\[vimeo\](.+?)\[/vimeo\]~s'
    );
    $replace = array(
        'GenerateAlbum($1)', // call a PHP function
        '<img src="$4" width="$1" height="$2" alt="$3" />',
        '<iframe src="http://www.youtube.com/embed/$1"></iframe>',
        '<iframe src="https://player.vimeo.com/video/$1"></iframe>'
    );
    return preg_replace($find, $replace, $text);
}
2
  • What's wrong with preg_replace_callback what went wrong? Can you show the code where you tried to use preg_replace_callback. Commented Mar 10, 2016 at 0:29
  • 1
    Run the rest as normal, run the album one by itself using the callback method. Commented Mar 10, 2016 at 0:30

1 Answer 1

0

If you wanted to call a function on more than one replacement or you wish to set up your script for future modifications so that functions can be called on the replacement parameter, you might entertain preg_replace_callback_array().

Otherwise, I'd say make a preg_replace_callback() involving the first elements of $find and $replace then run a call of preg_replace() on the remaining elements.

Code: (Demo)

function GenerateAlbum($match) {
    return "<div class=\"album\>Do whatever: " . strtoupper($match[1]) . "</div>";
}

function FormatModules($text) {
    $text = preg_replace_callback('~\[album\](.+?)\[/album\]~s', "GenerateAlbum", $text);
    $find = array(
        '~\[img width=(.*?) height=(.*?) alt=(.*?)\](https?://.*?\.(?:jpg|jpeg|gif|png))\[/img\]~s',
        '~\[youtube\](.+?)\[/youtube\]~s',
        '~\[vimeo\](.+?)\[/vimeo\]~s'
    );
    $replace = array(
        '<img src="$4" width="$1" height="$2" alt="$3" />',
        '<iframe src="http://www.youtube.com/embed/$1"></iframe>',
        '<iframe src="https://player.vimeo.com/video/$1"></iframe>'
    );
    return preg_replace($find, $replace, $text);
}

echo FormatModules("[vimeo]test1[/vimeo]\n\n[album]test2[/album]\n\njust text\n\n[img width=50 height=100 alt='sumpin good']http://www.example.com/image.gif[/img]\n\n[youtube]test3[/youtube]");

Output:

<iframe src="https://player.vimeo.com/video/test1"></iframe>

<div class="album\>Do whatever: TEST2</div>

just text

<img src="http://www.example.com/image.gif" width="50" height="100" alt="'sumpin good'" />

<iframe src="http://www.youtube.com/embed/test3"></iframe>
Sign up to request clarification or add additional context in comments.

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.