PHP Regex Get Text Between BBCode Tags

前端 未结 1 1029
时光取名叫无心
时光取名叫无心 2020-12-03 13:24

I need help with the PHP code for the following:

Get the text between each occurrence the BBCode tags [code] and [/code] in a given string so I can then replace the

相关标签:
1条回答
  • 2020-12-03 13:48

    I think you're searching for something like this

    <?php
    preg_match_all("/\[code\](.*?)\[\/code\]/ism", $search, $match);
    

    hover, I'd suggest you to use BBcode parsers instead


    To replace all spaces with &nbsp;, simply use preg_replace_callback

    <?php
    $text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism", function($match) {
         return str_replace(" ", "&nbsp;", $match[1]);
    }, $search);
    
    0 讨论(0)
提交回复
热议问题