duanjie2940 2015-11-12 16:57
浏览 22

来自strip_tags PHP的奇怪行为

I have some HTML as a string in PHP that I've used $html_str= html_entity_decode() on. So a sample of the HTML now looks like this:

echo $html_str; // <p>This is <span class='some_class'>some</span> text, yeah!<br>Hello world.</p>

When I do this:

$html_str= strip_tags($html_str, '<p>');
echo $html_str; // This is some text, yeah!Hello world.

it strips all tags including the <p> tags.

But if I do:

$html_str= strip_tags($html_str, '<br>');
echo $html_str; // <p>This is some text, yeah!<br>Hello world.</p>

it strips the <span> tags and leaves the <br> and the <p>.

What's going on here?

  • 写回答

1条回答 默认 最新

  • doris20141022 2015-11-12 17:03
    关注

    The second argument $allowable_tags says which tags are allowed:

    <?php
    
    $html_str = "<p>This is <span class='some_class'>some</span> text, yeah!<br>Hello world.</p>";
    
    echo strip_tags($html_str, '<p>') . "
    ";
    
    echo strip_tags($html_str, '<br>') . "
    ";
    

    Output is:

    <p>This is some text, yeah!Hello world.</p>
    This is some text, yeah!<br>Hello world.
    

    as expected.

    http://ideone.com/5R3VvZ

    The most likely explanation to me is that you don't really have <br> in your string but something else (is the HTML tag encoded?).

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100