remove spaces in bbcode Values aftar "]" AND befor "["

62 Views Asked by At

Is there a way to correct the input before it is used by trim() or other ??

The input must first be corrected From

[youtube]       IHu9-Q30Nuw     [/youtube] 
texttexttext


texttext

[code]               codecodecode  codecode codecodecode
codecodecode



codecodecode
[/code] 

to

[youtube]IHu9-Q30Nuw[/youtube] 
texttexttext


texttext

[code]codecodecode  codecode codecodecode
codecodecode



codecodecode[/code] 
1

There are 1 best solutions below

0
xrescom On

finally did it :)

input + code

// input -------------------
$OLDtext = "[youtube]       IHu9-Q30Nuw     [/youtube] 
texttexttext


texttext

[code]               codecodecode  codecode codecodecode
codecodecode



codecodecode
[/code] 
";

// code -------------------
$NEWtext = preg_replace_callback("/\[(.*?)\](.*?)\[\/(.*?)\]/ism"
                                , 
                                function($match) 
                                {
                                    $TEMPtext   = preg_replace('/\]\s+/', ']', $match[0]); 
                                    return preg_replace('/\s+(\[+)/', '$1', $TEMPtext);
                                }
                                , 
                                $OLDtext
                            );
// print -------------------
echo nl2br($NEWtext);

output

[youtube]IHu9-Q30Nuw[/youtube] 
texttexttext


texttext

[code]codecodecode  codecode codecodecode
codecodecode



codecodecode[/code]