doutui6241 2011-02-13 04:19
浏览 10
已采纳

过滤BBCodes的某些元素

It's easy to explain what I want to achieve, but for me (a novice at PHP), hard to actually achieve it. Basically what I want is to make BBCodes as easily and short as possible. Instead of an array like

$filter=array(
    '[b]'=>'<b>',
    '[/b]'=>'</b>',
    '[i]'=>'<i>',
    '[/i]'=>'</i>');

I'd like to have this array:

$filter=array('b','i');

Then, the part I can't get to, would be where it checks for the strings in that array to have brackets around them (and, another thing I can't figure out, to be able to check also for / in the bracket) and then replace those brackets with <>. So, [b] would become <b> and [/b] would become </b>.

Edit: Solution

function bbcode($string) {
    $filter=array('b','i','u');
    foreach ($filter as $filter) {
        $string=str_replace('['.$filter.']','<'.$filter.'>',$string);
        $string=str_replace('[/'.$filter.']','</'.$filter.'>',$string);
        }
    return $string;
    }
  • 写回答

1条回答 默认 最新

  • dongwei4096 2011-02-13 04:53
    关注
    $filter = array('b','i');
    
    $newfilter = array();
    foreach ($filter as $tag) {
      $newfilter["[$tag]"] = "<$tag>";
      $newfilter["[/$tag]"] = "</$tag>";
    }
    

    Now you can use $newfilter.

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

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画