2012-02-10 47 views
2

这里是我的正则表达式:PHP - 正则表达式是不是5.2的工作,工作在5.1和5.3

$table_pattern = "/<TABLE.*?>(.*?)<\/TABLE>/is"; 

就像标题所说,它工作在5.1和5.3,而不是5.2。我用它在这个preg_match

preg_match_all($table_pattern, $page_content, $table_content); 

$table_contentNULL在5.2,但人口5.1和5.3。任何想法为什么?

其他细节:

$car_count = 47; //How many cars are currently online 
$page_content = file_get_contents('http://www.site.com/temps/inventory.cfm?ChangeItems='.$car_count);; // What page will be parsed 
$page_start = 10277; //Where the parsing should start 

$page_content = substr($page_content, $page_start); //Removes all of the text above the table we need 
$table_pattern = "/\<TABLE.*?\>(.*?)\<\/TABLE\>/is"; 
preg_match_all($table_pattern, $page_content, $table_content); //Finds all tables inside of $page_content and fills the $table_content array 
$final_content = $table_content[0][0]; //Setting the first table, which is the match we need, to $table 

$ final_content快到了为NULL。显然,在我的代码中,在这个下面发生了更多的事情,但它是无关紧要的。

我解决了我自己的问题 - 等待它 - 不使用正则表达式!但实际上,我最初认为这比处理PHP Simple HTML Parser要快得多,但事实并非如此。但我仍然很好奇,为什么这在某些版本中不起作用。

+10

[小马他来...](http://stackoverflow.com/a/1732454/554546) – 2012-02-10 21:35:09

+0

转义'<' and '>'? – Halcyon 2012-02-10 21:36:00

+5

也许5.2很聪明,可以避免重新渲染xml。 :) – 2012-02-10 21:36:34

回答

0

可以使用递归正则表达式在PHP中解析XML,但请使用XML library而不是正则表达式。这是更清洁和更容易...

(如果你已经在你的XML嵌套的“表”你的代码没有...)

的PCRE是另一支球队比php开发的,和旧版本有错误。也许在PCRE中有一个错误传递给php 5.2,后者的版本会修正它。

另一种解释可能是,你有unicode xml,并且你没有使用“u”标志。

通过我它在PHP 5.2.17工程。你有哪个版本?

+0

我已经开始使用PHP Simple HTML DOM Parser来完成我所需要的功能。我不记得它是什么确切的版本,我将不得不再次检查。 – ohiock 2012-02-15 17:47:22