【问题标题】:`preg_match_all()` yields incorrect/null results`preg_match_all()` 产生不正确/空的结果
【发布时间】:2011-12-30 09:17:33
【问题描述】:

问题

为什么我会得到错误的结果?我怎样才能得到想要的结果?

输入文件

vulture (wing)
tabulations: one leg; two legs; flying
father; master; patriarch
mat (box)
pedistal; blockade; pilar
animal belly (oval)
old style: naval
jackal's belly; jester    slope of hill (arch)
key; visible; enlightened

两个版本的代码,有问题

版本 1:

<?php
$filename = "fakexample.txt";
$file = fopen($filename, "rb");
$myFile = fread($file, filesize($filename));
//PLEASE NOTE THAT $STARTSAFE IS IN PLACE IN THIS VERSION AND NOT IN THE NEXT
function get_between($startString, $endString, $myFile, $startSafe, $endSafe){
  //CHANGES WILL START HERE
  if($startSafe = 0){
    $startStringSafe = $startString;
  }
  elseif($startSafe = 1){
    $startStringSafe = preg_quote($startString, '/');
  }
  //AND END HERE
  if($endSafe = 0){
    $endStringSafe = $endString;
  }
  elseif($endSafe = 1){
    $endStringSafe = preg_quote($endString, '/');
  }
  //non-greedy match any character between start and end strings. 
  //s modifier should make it also match newlines.
  preg_match_all("/$startStringSafe(.*?)$endStringSafe/m", $myFile, $matches);
  return $matches;
}
$list = get_between("^", ")", $myFile, 0, 1);
foreach($list[1] as $list){
  echo $list."\n";
}
?>

此代码不返回任何内容。

版本 2:

<?php
$filename = "fakexample.txt";
$file = fopen($filename, "rb");
$myFile = fread($file, filesize($filename));
//PLEASE NOTE THAT $STARTSAFE IS NOT IN PLACE IN THIS VERSION
function get_between($startString, $endString, $myFile, $startSafe, $endSafe){
  //CHANGES START HERE

    $startStringSafe = $startString;



  //AND END HERE
  if($endSafe = 0){
    $endStringSafe = $endString;
  }
  elseif($endSafe = 1){
    $endStringSafe = preg_quote($endString, '/');
  }
  //non-greedy match any character between start and end strings. 
  //s modifier should make it also match newlines.
  preg_match_all("/$startStringSafe(.*?)$endStringSafe/m", $myFile, $matches);
  return $matches;
}
$list = get_between("^", ")", $myFile, 0, 1);
foreach($list[1] as $list){
  echo $list."\n";
}
?>

这将返回vulture (wing mat (box animal belly (oval jackal's belly; jester slope of hill (arch

想要的输出

vulture mat animal belly jackal's belly; jester slope of hill

显着差异

vulture **(wing** mat **(box** animal belly **(oval** jackal's belly; jester slope of hill **(arch**

【问题讨论】:

  • 您可以使用rubular.com 来测试您的正则表达式。很有帮助!
  • 可能是因为我用的是file_get_contents(),所以只有一行?

标签: php regex serialization preg-match-all recursive-regex


【解决方案1】:

^ 表示默认主题的开始。如果您希望它表示行首,则必须在您的正则表达式包装器中使用 /m modifier

【讨论】:

  • 所以我基本上必须使用/m 而不是/s?我得到空结果。
  • 您可以组合这两个标志。那是/ms
  • 奇怪的是,无论我使用/m 还是/s,当我使用参数化的$startString 时我得到一个空结果,当我不使用时我得到vulture (wing mat (box animal belly (oval jackal's belly; jester slope of hill (arch .两者都不正确。正确的应该是vulture mat animal belly jackal's belly; jester slope of hill。所以,它在错误的字符处中断。
  • 好吧,我不知道您的繁琐功能是什么,您也没有详细说明您的输入。但您可能想查看if 语句。里面有赋值表达式,而不是比较。
  • get_between("^", ")" 将剪切到右括号,而不是您假设使用的左括号。仍然不正确的分配。
猜你喜欢
  • 1970-01-01
  • 2020-12-14
  • 2015-10-12
  • 2015-01-06
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
相关资源
最近更新 更多