donglulong0877 2017-01-04 10:53
浏览 190
已采纳

PHP正则表达式 - 删除所有<br>标签

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条回答 默认 最新

  • dpnru86024 2017-01-04 11:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改