PHP Regex - Remove all <br> tag

647 views Asked by At

i have a function

    function showBBcodes($text) {
  // BBcode array
  $find = array(
    '~\[b\](.*?)\[/b\]~s',
    '~\[i\](.*?)\[/i\]~s',
    '~\[u\](.*?)\[/u\]~s',
    '~\[quote\](.*?)\[/quote\]~s',
    '~\[size(.*?)\=(.*?)\](.*?)\[/size\]~s',
    '~\[color(.*?)\=(.*?)\](.*?)\[/color\]~s',
    '~\[url\]((?:ftp|https?)://.*?)\[/url\]~s',
    '~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s',
    '~\[b_id\](.*?)\[/b_id\]~s',
    '~\[tex\](.*?)\[/tex\]~s',
    '~\[sup\](.*?)\[/sup\]~s'
  );
  // HTML tags to replace BBcode
  $replace = array(
    '<span stype="font-weight: bold;">  $1</span>',
    '<i>$1</i>',
    '<span style="text-decoration:underline;">$1</span>',
    '<pre>$1</'.'pre>',
    '<span style="font-size:$2px;">$3</span>',
    '<span style="color:$2;">$3</span>',
    '<a class="blue-font" href="$1">$1</a>',
    '<a class="blue-font" href="$1"><img src="$1" alt=""></a>',
    '<sub>$1</sub>',
    '<a class="blue-font" href="http://jabber.pozitiv-r.ru/cgi-bin/mathtex.cgi?$1"><img src="http://jabber.pozitiv-r.ru/cgi-bin/mathtex.cgi?$1" alt=""></a>',
    '<sup>$1</sup>'
  );
  // Replacing the BBcodes with corresponding HTML tags
  return preg_replace($find,$replace,$text);
} 

All i need is delete all </br> from [tex][/tex] tag. I tired to modify my regexp like ^<br.*?>#s but it doesn't work.

1

There are 1 answers

0
lndim On BEST ANSWER

Try something like this^

$text = preg_replace_callback(
    '~\[tex\].*?\[/tex\]~s',
    function ($matches) {
        return preg_replace('~<br.*?>~', '', $matches[0]);
    },
    $text
);

Add the peace of code before the return statement.