1

Hello guys i have the follow code on php

return preg_replace_callback("#\{gallery: '(.+?)'\}#i", 
    function($i)
    { 
        $oGallery = new Gallery( $i[1] );
        $oGallery->PublicSide();
    }, $string);

Now i want to add the following to it:

Say that that i want to allow the user to choose if he wants to add directories to the gallery so i do:

"#\{gallery: '(.+?)' dir: '(.+?)')\}#i"

so the user has the potion to do: {gallery: 'folder'} or {gallery: 'folder' dir: '1'}

how would i go about doing this in terms of regular expressions so the replacement takes place even if the directory option isnt there? thanks in advance!

1 Answer 1

1

Make it optional

"#\{gallery: '(.+?)'(?: dir: '(.+?)')?\}#i"

(?:text) creates a non-capturing group

adding ? after the group makes it optional

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

1 Comment

One more question, this works on my PHP 5.3 localhost but it doesn't seem to work on my 5.2.17 server, seems to be something with the single quotes, any idea whats going on?

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.