【问题标题】:Preg_Match doesn't work correctlyPreg_Match 无法正常工作
【发布时间】:2015-07-20 16:46:56
【问题描述】:

出于某种原因,这(乱七八糟的东西)不起作用..它很乱,因为我改变了很多以找出问题所在..

问题:preg_match("/\"h4(.*?)/h4/s", $theMAGICresult, $output_game);

我知道比赛有效..(通常);我确实检查了 http://www.phpliveregex.com/。我在foreach循环,while循环中做到了.. 我尝试 preg_match_all .. 我提供了一个正确的模式,一个正确的字符串 对于主题,我把它放在一个正确的数组中并获取它,但它是 空..在找到答案2小时后我放弃了..你能帮我吗 ??

preg_match_all("/<li class=\"completed\">(.*?)<\/li>/s", $all, $output);
/* $all contains something like        xx <li class="completed"> xxxx"h4GOLDFISH/h4xxxxxx</li>xxxxxxxxxxxx */
$arrayLoop = array(); //makes sure it's an array
$arrayLoop = $output[0]; // makes $output[0] the default array

//$arrayLoop[0] is now <li class="completed"> xxxx"h4GOLDFISH/h4xxxxxx</li>
$i = 1; //while loop because, had first foreach loop etc..
while($i < count($arrayLoop)) {
$theMAGICresult = $arrayLoop[$i]; // to be sure $arrayLoop[$i] is valid

//theMAGICresult is now : <li class="completed">xx"h4GOLDFISH/h4xxxxxx</li>
preg_match("/\"h4(.*?)\/h4/s", $theMAGICresult, $output_game);

echo $output_game[1]; //is nothing while is should have GOLDFISH

    $i++;
}
print_r($output_game); //empty array

【问题讨论】:

  • 如果我不得不猜测,我会说你正在尝试解析一些 HTML。您尝试使用正则表达式但卡住了,现在您正试图强制执行您认为的解决方案。这被称为XY problemwhy you can't parse HTML with regular expressions 也有很好的解释。你试过 PHP 的DOMDocument 吗?如果没有,您应该这样做。
  • @N.B.谢谢,但我将 html 作为字符串处理... //string(722) 我什至从 H4 中删除了 '' 以确保它是一个字符串..
  • @Siguza 谢谢..现在我知道这不是 html 的东西..我猜我看错了..

标签: php preg-match


【解决方案1】:

尝试:

preg_match("/\\"h4(.*?)\/h4/s", $input_line, $output_array);

固定示例:

$output_game = [];
$theMAGICresult = '<li class="completed">xx"h4GOLDFISH/h4xxxxxx</li>';
preg_match('/\"h4(.*?)\/h4/s', $theMAGICresult, $output_game);
var_dump($output_game);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    相关资源
    最近更新 更多