【问题标题】:PHP Regex - Remove all <br> tagPHP 正则表达式 - 删除所有 <br> 标签
【发布时间】:2017-01-04 10:53:05
【问题描述】:

我有一个功能

    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);
} 

我只需要从 [tex][/tex] 标记中删除所有 &lt;/br&gt;。我厌倦了像^&lt;br.*?&gt;#s 这样修改我的正则表达式,但它不起作用。

【问题讨论】:

标签: php regex bbcode


【解决方案1】:

试试这样的吧^

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

return 语句之前添加代码和平。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多