【问题标题】:decode string between special tags with htmlspecialchars_decode and preg_replace使用 htmlspecialchars_decode 和 preg_replace 解码特殊标签之间的字符串
【发布时间】:2012-06-16 04:04:12
【问题描述】:

我正在尝试使用htmlspecialchars_decodepreg_replace 解码两个特殊标签之间的字符串,例如我的原始字符串是这样的:

other strings...[link]<a href="http://example.com" target="_blank">example</a>[/link]other strings...

我需要将 [link][/link] 之间的所有内容转换为 html 原始代码。对此进行了测试:

    $str = preg_replace("/[link](.*)[\/link]/eisU", "htmlspecialchars_decode('$1')", $str);                 

没有用!我也搜索了 google 和 SO 但没用!

【问题讨论】:

    标签: php html string replace special-characters


    【解决方案1】:

    您必须转义方括号,否则 [link] 会被解释为包含字母 l, i, n, k 的字符集。

    如果你不确定要转义什么,你应该使用preg_quote()

    preg_replace('/' . preg_quote('[link]', '/') . '(.*?)' . preg_quote('[/link]', '/') . '/eisU', ...
    

    这也可以:

    /\[link\](.*)\[\/link\]/eisU
    

    【讨论】:

    • 是的,我试图接受你的回答,但时间已经结束了! :-) 对不起,朋友。我刚给你的答案打分。
    • @Vahid 我不是说在计时器到点之前哈哈……你现在可以接受了 :)
    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多