我可以帮您翻译如下内容,涉及it技术。
我有一段代码可以生成字符串数组... 现在我的问题是我需要截取数组中每个结果的子串,但我认为不能在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是一个数组,我需要对$matches中的每个结果进行子字符串处理。
array_map
(返回一个新数组)或array_walk
(可以改变原始数组中的项)。 - undefined