【问题标题】:preg_match_all reading from file returns empty arraypreg_match_all 从文件读取返回空数组
【发布时间】:2018-11-21 18:37:32
【问题描述】:

我正在尝试读取文件并过滤其内容。我期待的结果是:https://regex101.com/r/uMTrtd/3 但我不知道为什么我没有得到它。我尝试更改 PCRE 限制但没有结果,我不知道还能做什么。

如果我使用实际字符串运行 preg 而不是读取文件,它可以工作,但 var_dump($file) 返回一个字符串。

<?php

$file = file_get_contents('myfile.txt');
var_dump($file);
$text = preg_replace('/(cve: .+? Pág\. [0-9]{1,2} )/u','', $file);

$re = '/(La señora|El señor) (.+?):(.+?\. (?=(La señora|El señor) (.+?):|Eran las .+?\.))/u';
preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0);
var_dump($matches);

?>

你可以在这里查看文件内容https://regex101.com/r/uMTrtd/3

【问题讨论】:

  • 代码工作,你应该看看文件是如何保存和编码的。
  • 我怎么能看到呢?该文件以 UTF-8 编码
  • 您下载了文件吗?我以为你自己复制/粘贴并保存文件。
  • 我通过PHP生成文件
  • 嗯,这是我给你的建议,对你没有帮助。代码在我的机器上运行良好。

标签: php file preg-replace file-get-contents preg-match-all


【解决方案1】:

试试这个:

$text = preg_replace('/(cve: .+? Pág\. [0-9]{1,2} )/us','', $file);


$re = '/(La señora|El señor) (.+?):(.+?\. (?=(La señora|El señor) (.+?):|Eran las .+?\.))/us';

"s" 使其也搜索换行符

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2015-12-24
    • 2021-08-19
    相关资源
    最近更新 更多