doue1925 2012-08-22 23:21
浏览 24
已采纳

PHP Preg_replace删除所有xml

I am having a text area in my UI and I would like to filter out all xml(including html) text entered by user using preg_replace. I need a some help with it.

Eg:

Emailed user sam at 12:15 PM
<details><email>sam@sam.com</email></details>

After preg_replace :

Emailed user sam at 12:15 PM

Along with this any pointers to preg_replace regex would be great. I tried using some java regex(I am new to PHP) in preg_replace and found that it did not work. I am not sure if my regex was wrong or preg_replace uses different kind of regex.

All your help is much appreciated.

  • 写回答

1条回答 默认 最新

  • doumeng06063991 2012-08-22 23:33
    关注

    mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

    So something like the following will work in most cases,

    $str_out = preg_repace('/<[\/]{0,1}\w[^>]*>/g', '', $str_in);
    

    Where '/<[\/]{0,1}\w[^>]*>/g' is regex for <name stuff> and </name stuff>. It will fail if you've a > in an attribute (i.e. <name blah=">hello"> will result in hello">

    Although strip_tags is native so will be faster. Remember the arguments for strip_tags are the things to not strip


    EDIT: If you want the content between the tags stripped out too, the regex for that is '/<\w[^>]*>[\s\S]*?<\/\w[^>]*>/g', but this will fail in even more situations (i.e. badly closed/nested tags), so you might want to run it first then the above on the result to make sure.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常