2016-04-25 83 views
-1

这是我regex101例如其中的工作原理:https://regex101.com/r/fE6rO9/2(你不得不等待几个secondes,因为它的大)的preg_match正则表达式不会找到匹配(但它适用于regex101)

这里是我的PHP

$content = htmlentities($contentCode); 
    /* correct echo when i copy paste it into regex101 */ 
    echo $content; 

    // copy past from regex101 
    $re = "/<\\/form><table class=\"forumline\" width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">(.*)<\\/table><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">/"; 

    preg_match($re, $content, $matches); 

    var_dump($matches); 

但我运行它时,它回响:

array (size=0) 
    empty 

任何想法是什么问题是?

这是正则表达式采取APPART:

"/<\\/form><table class=\"forumline\" width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">(.*)<\\/table><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">/"; 
+0

请发布明确的*** ***输入和期望的输出*** ***示例 –

+3

主要问题是您使用正则表达式来解析html。花时间学习如何使用DOMDocument。 –

+0

所需的输出是在该链接:https://regex101.com/r/fE6rO9/2 =>我期望1输出,1匹配 – Couteau

回答

2

我测试过你的正则表达式,它似乎如果删除

$content = htmlentities($contentCode); 

工作简单地使用:

$content = $contentCode; 

备注:

  1. 请务必阅读You can't parse [X]HTML with regex
  2. 一些替代regexDOMDocumentsimplehtmldom
+0

哈哈!我不会使用Regex来解析HTML。我知道如何使用jquery获取所有信息,但是因为我使用PHP,还有另一种检索信息的方式吗? – Couteau

+1

你可以使用php [DOMDocument](https://secure.php.net/manual/en/class.domdocument.php)或[simplehtmldom](http://simplehtmldom.sourceforge.net/) –

+0

哦,好吧,那很漂亮强大的功能来获取每个节点的每个信息。但是当我用$ contentCode加载HTML时,我收到了很多错误。用我的正则表达式,我只需要我需要的html部分,并使用DOM文档,我可以非常轻松地访问每一行。 – Couteau