douyan8772 2016-02-18 17:41
浏览 12
已采纳

PHP preg匹配不起作用

I have a str from witch I want to parse all <li></li> tags, this is the string.

<li>Want this</li>DON'T WANT THIS<li>Want this</li>DON'T WANT THIS<li>Want this</li>...

This is the code that I'm using:

$my_text= array();
preg_match('/<li>(.*?)<\/li>/', $str, $my_text);

But it doesn't work. When I run it this is my_text array:

[0] => "<li>Want this</li>"
[1] => "Want this"

It only has 2 elements of 1000 of them.

  • 写回答

4条回答 默认 最新

  • dtmwnqng38644 2016-02-18 17:51
    关注

    All you need is using preg_match_all() function, Something like this:

    <?php
    
    $str = "<li>Want this</li>DON'T WANT THIS<li>Want this</li>DON'T WANT THIS<li>Want this</li>";
    preg_match_all('/<li>(.*?)<\/li>/', $str, $out);
    echo '<pre>';
    print_r($out);
    

    Online Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ENVI高分五号去除云层的方法
  • ¥15 16进制数据如何得到奇偶校验位
  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系