开发者

filter php variable for specific bbcode string - wrap matches inside of divs?

开发者 https://www.devze.com 2023-02-20 08:49 出处:网络
hey guys, my php variab开发者_开发知识库le $content holds html! i want to filter this $content for

hey guys, my php variab开发者_开发知识库le $content holds html! i want to filter this $content for

[q=SomeQuestoin] and [a=SomeAnswer]

and wrap each match inside of a div.question and div.answer.

So whenever this [q=Some Question][a=Some Answer] structure is found in $content i want to put out this.

<div class="qanda">
    <div class="question">
        Some Question
    </div>

    <div class="answer">
        Some Answer
    </div>  
</div>

Is that possible? Important is that the Qustion Text or the Answer Text could hold html tags as well. like <p> or <b> etc.

update:

    $q_regex = '/\[q=([^"]+?)]/is'; 
    $q_output = '<div class="qanda"><div class="queston">$1</div>'; 
    $content = preg_replace($q_regex, $q_output, $content);

    $a_regex = '/\[a=([^"]+?)]/is'; 
    $a_output = '<div class="answer">$1</div></div>'; 
    $content = preg_replace($a_regex, $a_output, $content);


http://www.spotlesswebdesign.com/blog.php?id=12

tutorial on using regex to do bbcode parsing. people would recommend using a bbcode parser module however. should be safe to regex since you are not using nesting and whatnot.

EDIT

possible but tricky. could be error prone. something like this maybe:

$result = preg_replace('/\[q=(.+?)].+?\[a=(.+?)]/is', '<div class="qanda"><div class="question">$1</div><div class="answer">$2</div></div>', $subject);
0

精彩评论

暂无评论...
验证码 换一张
取 消