php regex [b] to <b>

余生颓废 提交于 2019-11-26 17:34:31

问题


  "'\[b\](.*?)\[/b\]'is",

Is my current RegEx working. But I want to change the [] to be <> instead. But it doesn't work... What more then just the [] do I need to change.


回答1:


There are various BBCode parsers available for PHP, for instance

  • http://www.php.net/manual/en/book.bbcode.php

which allows you to simply define your replacement rules by hand:

echo bbcode_parse(
    bbcode_create(
        array(
            'b' => array(
                'type'      => BBCODE_TYPE_NOARG,
                'open_tag'  => '<b>',
                'close_tag' => '</b>'
            )
        )
    ),
    '[b]Bold Text[/b]'
);
// prints <b>Bold Text</b>

Also check the various similar questions about BBCode Parsers:

  • https://stackoverflow.com/search?q=bbcode+php



回答2:


Try ~ as a delimiter instead

preg_match("~<b>(.*?)</b>~is", $text, $b);


来源:https://stackoverflow.com/questions/7545920/php-regex-b-to-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!