PHP预匹配所有内容,读取内容并排除不需要的内容
我有一个要读取的文本文件,但不包括开头包含特定字符的行(因此为“@”或后面定义的任何字符): 使用下面的代码,我可以分解以分号(;)结尾的代码,但最后一行不应该分解,因为它以“@”开头 有没有达到这个目的的暗示?谢谢 更新代码:PHP预匹配所有内容,读取内容并排除不需要的内容,php,Php,我有一个要读取的文本文件,但不包括开头包含特定字符的行(因此为“@”或后面定义的任何字符): 使用下面的代码,我可以分解以分号(;)结尾的代码,但最后一行不应该分解,因为它以“@”开头 有没有达到这个目的的暗示?谢谢 更新代码: // http://stackoverflow.com/questions/10257244/php-preg-match-all-read-content-and-exclude-unwanted/10257319 $results = array(); $m
// http://stackoverflow.com/questions/10257244/php-preg-match-all-read-content-and-exclude-unwanted/10257319
$results = array();
$matches = array();
$the_path = '/path/to/file.txt';
if (is_file($the_path)) {
$contents = file_get_contents($the_path);
if ($contents) {
// ! array warning
// $contents = array_map('rtrim', $contents);
// $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT);
$matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY);
if ($matches) {
foreach ($matches as $key => $val) {
$results[$key] = $val;
}
}
}
}
// Attempt to remove the first 0 key, and start from 1, because 0|value0 is considered NULL
$results = array_combine(range(1, count($results)), array_values($results));
return !empty($results) ? $results : array();
$matches = array();
if ($contents = file($the_path)) {
$contents = array_map('rtrim', $contents);
$keyword = '@';
// Still output @line
// $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT);
// Ok, thanks to http://php.net/manual/de/function.preg-grep.php#85503
$matches = preg_grep("/{$keyword}/i", $contents, PREG_GREP_INVERT);
// $matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY);
// dsm($matches);
if ($matches) {
foreach ($matches as $key => $match) {
$results[$key] = $match;
}
}
}
// $results = array_combine(range(1, count($results)), array_values($results));
return $results;
更新2,通过DCoder正常工作:
// http://stackoverflow.com/questions/10257244/php-preg-match-all-read-content-and-exclude-unwanted/10257319
$results = array();
$matches = array();
$the_path = '/path/to/file.txt';
if (is_file($the_path)) {
$contents = file_get_contents($the_path);
if ($contents) {
// ! array warning
// $contents = array_map('rtrim', $contents);
// $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT);
$matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY);
if ($matches) {
foreach ($matches as $key => $val) {
$results[$key] = $val;
}
}
}
}
// Attempt to remove the first 0 key, and start from 1, because 0|value0 is considered NULL
$results = array_combine(range(1, count($results)), array_values($results));
return !empty($results) ? $results : array();
$matches = array();
if ($contents = file($the_path)) {
$contents = array_map('rtrim', $contents);
$keyword = '@';
// Still output @line
// $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT);
// Ok, thanks to http://php.net/manual/de/function.preg-grep.php#85503
$matches = preg_grep("/{$keyword}/i", $contents, PREG_GREP_INVERT);
// $matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY);
// dsm($matches);
if ($matches) {
foreach ($matches as $key => $match) {
$results[$key] = $match;
}
}
}
// $results = array_combine(range(1, count($results)), array_values($results));
return $results;
使用此代码,$行将包含一个数组,其中包含所有不以
@
$contents = file_get_contents($the_path);
$lines = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), null, PREG_SPLIT_NO_EMPTY);
谢谢,但仍然会收到数组警告,可能在我的代码中的某个地方。警告:array#u map()[function.array map]:参数#2应该是一个数组。请确保您尝试打开的文件确实存在(
文件
如果无法读取,则返回FALSE)。是的,is#u文件(\u路径)为usual@swan:现在您已经添加了新代码,错误变得更有意义:您正在执行$contents=file\u get\u contents(\u路径);代码>,但我的代码示例正在执行$contents=file(_路径)代码>。您的版本在单个字符串中返回整个文件,我的版本返回一个数组(每行作为一个单独的元素)。奇怪的是,以前的它(删除带有@的行)工作正常,除了已修复的数组之外,但现在在所有缓存工作正常后都会出现@line,除了它添加了一个不需要的空0 |“”对之外,您能否提供它无法正常工作的完整数据?您知道如何删除最后的0 |“”对吗。array\u shift使结果无效。顺便说一句,array\u unshift不会使所有结果无效。你可能用错了。只需执行array\u unshift($results)代码>,而不是$results=array\u unshift($results)代码>谢谢。补充说,你是如何在那里超越PHP的?不允许?只需使用数组\u shift($results);移除第一个入口谢谢。以前曾尝试过,最后得到了那条难看的线谢谢,我总是接受答案,除了那些没有答案的:(。所以我的答案既不是答案,也没有帮助你?几乎有效,除了空的0 |“”对。使用array|u shift()会使它们全部无效。
$contents = file_get_contents($the_path);
$lines = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), null, PREG_SPLIT_NO_EMPTY);