tags:

views:

53

answers:

2

I have this code:

$text = '[iframe=200x200]http://stackoverflow.com[iframe] ';
$text = preg_replace(
    '/\[iframe=(.*?)x(.*?)\](.*?)\[\/iframe\]/ms', 
    '<iframe style="border: 1px solid rgb(204, 204, 204); width: \1px; height: \2px;" src="\3"></iframe>', 
    $text
);
echo $text;

Why is it not working?

+1  A: 

Try:

$text = preg_replace('/\[iframe=(.*?)x(.*?)\](.*?)\[iframe\]/ms',
        '<iframe style="border: 1px solid rgb(204, 204, 204); width: \1px; height: \2px;" src="\3"></iframe>',
        $text);

There were some unwanted slashes in \[\/iframe\] which needed to be changed to \[iframe\]

EDIT:

Actually your input string looks incorrect, as it does not have a closing iframe tag:

$text = '[iframe=200x200]http://stackoverflow.com[iframe] ';

should be

$text = '[iframe=200x200]http://stackoverflow.com[/iframe] ';

In cases where your string contains / you can make use of some other delimiter to make avoid escaping / found in the string. Something like:

$text = preg_replace('#\[iframe=(.*?)x(.*?)\](.*?)\[/iframe\]#ms',
            '<iframe style="border: 1px solid rgb(204, 204, 204); width: \1px; height: \2px;" src="\3"></iframe>',
            $text);
codaddict
oh god its worked thanks very much ,but what did you do
moustafa
+2  A: 

Your input string has an error. The / at the closing tag is missing

[iframe=200x200]http://stackoverflow.com[/iframe]

harpax
thank you i fixed it
moustafa