dongshou1856 2015-01-21 11:04
浏览 131

preg_replace:正则表达式不起作用

<h1>title 1</h1 w:id="0"/><p>content</p><h1>title 2</h1 w:id="1"/>...

I want to replace w:id="0"/ from </h1 w:id:="0 or 1 ect "/>

I use this code:

preg_replace("</h1 (.*?)>",'',$html)

But it doesn't work anymore

  • 写回答

2条回答 默认 最新

  • douyi1939 2015-01-21 11:09
    关注

    You are missing the delimiters on your regex.

    A regex needs a 'starting mark' and a similar 'finishing mark', so PHP can interpret it's content as the match, with all it's flags.
    Without the recognized delimiters, it's impossible to diferenciate between simple text and a regex.

    Try this regex:

    @</h1 (.*?)>@
    

    Or:

    ~</h1 (.*?)>~
    

    PHP supports a few more delimiters, like <> and /.


    As a side note, I would suggest the following regex:

    ~</h1( [^>]+)?>~i
    
    评论

报告相同问题?

悬赏问题

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