Php 我的正则表达式不能存储2个值有什么问题?

Php 我的正则表达式不能存储2个值有什么问题?,php,regex,Php,Regex,在某些情况下,这很好,在其他情况下,如下面,它不是 $xml_url = 'http://campusdining.compass-usa.com/Hofstra/Pages/SignageXML.aspx?location=Student%20Center%20Cafe'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $xml_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_s

在某些情况下,这很好,在其他情况下,如下面,它不是

$xml_url = 'http://campusdining.compass-usa.com/Hofstra/Pages/SignageXML.aspx?location=Student%20Center%20Cafe';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.3a5pre) Gecko/20100526 Firefox/3.7a5pre");
$data = curl_exec($ch);
$ce = curl_error($ch);
curl_close($ch);

// this is how I was doing it prior to today and it worked before
// preg_match_all("/<MealPeriod name=\"(.+?)\">([\w\W\r\n]*?)<\/MealPeriod>/i", $data, $output_array);

// this way doesnt show all the meal periods, 
// but I need to know whats in between the MealPeriod tags
// preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array); 

// shows all the meal period names, 
// but I need the above to work to store whats in between the MealPeriod tags in the $output_array[2]
preg_match_all('/<MealPeriod name="(.*?)">/i', $data, $output_array); 

echo '<pre> '.print_r($output_array[1],1).'</pre>';
但它也应该保存$output_数组中MealPeriod标记之间的内容[2]


任何帮助都将不胜感激

下面的代码有效,我所做的只是更改正则表达式和打印

屏幕上的输出看起来很奇怪,因为第二个是什么?要捕获和之间的所有内容,还需要捕获所有xml标记。如果您查看源代码,您可以清楚地看到这一点

我鼓励您使用XML解析器来处理文档。在使用解析器将XML文档转换为对象之前,我当然使用过正则表达式来提取XML文档的某些部分,但与正则表达式相比,解析器能够更好地处理XML

所有内容都被捕获,但没有用标签打印到屏幕上。然而,如果你看来源,一切都在那里


多亏了下面的堆栈溢出帖子,我找到了答案-

我需要将正则表达式更改为以下内容,最终我能够在正确的数组中返回所有的用餐时间和内容

“/*?/我”


母鸡?它怎么不起作用?错误的结果是什么样子的?不管它值多少钱,regex在Rey上工作得很好:当我进行preg_match_all'/.*?/I',$data,$output_数组;然后打印_r$output_array[1];我只看到早餐出现了,它应该每天都出现,外卖,还有沙拉吧。这是一个不同的正则表达式,你问题中的那个仍然有效。事实上,在最新的链接中,它只显示早餐时段,需要显示所有4个时段——早餐、每天、外卖和沙拉吧
 Array
(
    [0] => Breakfast
    [1] => Every Day
    [2] => Outtakes
    [3] => Salad Bar
)
<?php
$xml_url = 'http://campusdining.compass-usa.com/Hofstra/Pages/SignageXML.aspx?location=Student%20Center%20Cafe';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.3a5pre) Gecko/20100526 Firefox/3.7a5pre");
$data = curl_exec($ch);
$ce = curl_error($ch);
curl_close($ch);

// this is how I was doing it prior to today and it worked before
// preg_match_all("/<MealPeriod name=\"(.+?)\">([\w\W\r\n]*?)<\/MealPeriod>/i", $data, $output_array);

// this way doesnt show all the meal periods, 
// but I need to know whats in between the MealPeriod tags
// preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array); 

// shows all the meal period names, 
// but I need the above to work to store whats in between the MealPeriod tags in the $output_array[2]
preg_match_all('/<MealPeriod name="(.*?)">(.*?)<\/MealPeriod>/i', $data, $output_array); 

echo '<pre> '.print_r($output_array,1).'</pre>';
?>