【发布时间】:2013-09-05 07:48:07
【问题描述】:
我有一个代码可以生成字符串数组......现在我的问题是我需要对数组的每个结果进行 substr,但我认为数组不允许在 substr 中使用......
请帮忙:
代码:
<?php
$file = 'upload/filter.txt';
$searchfor = $_POST['search'];
$btn = $_POST['button'];
$sum = 0;
if($btn == 'search') {
//prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
$result = implode("\n", $matches[0]);
echo $result;
}
else{
echo "No matches found";
}
}
?>
那里的 $matches 是数组...我需要 substr 每个 $matches 的结果
【问题讨论】:
-
要将函数应用于数组中的所有项,请使用
array_map(返回一个新数组)或array_walk(可以改变原始数组中的项)。 -
首先你的问题不清楚,你在哪个数组上使用 substr。你有没有尝试过? .任何数组应用函数的好解决方案是array_map
-
嗨@Jon 感谢您的回复...如果您不介意,您可以提供示例代码吗:3
-
@BOSS 我需要对 $matches 数组的每个结果进行 substr...