foreach循环不使用正则表达式

时间:2014-02-15 00:06:13

标签: php regex

我的文件images.html包含多行,代码如下:

<linestart><urlstart>http://...image.jpg<urlend><idstart>1<idend><lineend>

我想解析文件,但我无法弄清楚我的错误。

我的PHP代码:

$pattern = "/<linestart>(.*?)<lineend>/s";
$html = file_get_contents('images.html');

$check = preg_match_all($pattern,$html,$match);

foreach($match[1] as $line)
{ 
$pattern2 = "/<urlstart>(.*?)<urlend>/s";
$check2 = preg_match_all($pattern2,$line,$match_url);

$pattern3 = "/<idstart>(.*?)<idend>/s";
$check3 = preg_match_all($pattern3,$line,$match_id);


echo $match_url." id= ".$match_id."<br>";
}

我的结果是:

  

Array id= Array
  Array id= Array
  Array id= Array
  Array id= Array
  Array id= Array
  Array id= Array

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

您可以在一种模式中匹配所有内容:

$pattern = "/<linestart>.*?<urlstart>(.*?)<urlend>.*?<idstart>(.*?)<idend>.*?<lineend>/s";
$html = file_get_contents('images.html');

$check = preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    echo $match[1] . " id=" . $match[2];
}