我想从PHP中的字符串html提取数据

时间:2018-11-14 12:27:44

标签: php

我有HTML字符串,我想从该字符串中提取一些数据。

<html>
<head>
</head
<body>
<!-- [slider id=1] --> 
<!-- [slider id=2] -->
</body>
</html>

我想获取所有ID,然后相应地显示滑块。 在使preg_match进入循环后,我尝试了substrstrpos,但没有成功

$str = $response->content();

        $needle1 = "[slider";
        $needle2 = "] -->";
        $lastPos1 = 0;
        $lastPos2 = 0;
        $ids = array();
        while (($lastPos1 = strpos($str, $needle1, $lastPos1))!== false && ($lastPos2 = strpos($str, $needle2, $lastPos2))!== false) {

            echo $lastPos1. ' '.$lastPos2. ' '. substr($str, $lastPos1, $lastPos2).'<br/>';

            $lastPos1 = $lastPos1 + strlen($needle1);

            $lastPos2 = $lastPos2 + strlen($needle2);


        }
Output:

2152 2164 [slider id=1] --> 
2192 2204 [slider id=2] --> 
2231 2243 [slider id=3] --> 
2270 2282 [slider id=4] --> 

0 个答案:

没有答案