【问题标题】:search for string in text file using PHP使用 PHP 在文本文件中搜索字符串
【发布时间】:2021-05-02 07:39:25
【问题描述】:

我正在尝试使用 preg_match 在 .txt 文件中搜索一个短语,但它从来没有给我匹配。奇怪的是,如果我尝试在沙箱中复制和粘贴文件内容,我会得到很好的匹配。这是我尝试过的:

$txtUrl='https://www.sec.gov/Archives/edgar/data/1411579/000110465920131796/0001104659-20-131796.txt';
$getTextS3 = file_get_contents($txtUrl, true);
$str=strip_tags($getTextS3);
 $re = '/Shares of Class A common stock/i';
  preg_match_all($re, $str, $match);
  var_dump($match);

【问题讨论】:

  • 你能给我们一个你正在搜索的文本样本吗?

标签: php regex file search preg-match-all


【解决方案1】:

您找不到任何内容,因为“Shares of Class”和“A common stock”之间有一个转义的不间断空格 ( )。你必须在你的正则表达式中包含这个不间断的空间。我建议您在执行正则表达式(带有preg_match_all 的行)之前编写echo($str); 以将字符串视为调试步骤。

在你搜索这样的字符串之前,你应该仔细分析它。

【讨论】:

  • 添加了 str_replace("\xc2\xa0", ' ',$str) 并解决了。谢谢
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 2010-12-10
  • 1970-01-01
  • 2015-11-02
相关资源
最近更新 更多