duanne9313
2013-09-05 07:48
浏览 139
已采纳

数组PHP的substr()函数

I have a code that will produce array of strings.... now my problem is i need to substr each result of the array but i think array is not allowed to be used in substr...

please help:

CODE:

<?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:
";
$result = implode("
", $matches[0]);
echo $result;


 }
else{
 echo "No matches found";
 }


 }
 ?>

The $matches there is the array... i need to substr each result of the $matches

图片转代码服务由CSDN问答提供 功能建议

我有一个会产生字符串数组的代码....现在我的问题是我需要对每个结果进行substr 数组,但我认为数组不允许在substr ... ...

请帮助:

代码: \ n

 &lt;?php 
 $ file ='upload / filter.txt'; 
 $ searchfor = $ _POST ['search']; 
 $ btn = $ _POST ['button'  ]; 
 $ sum = 0; 
 
if($ btn =='search'){
 
 //阻止浏览器将其解析为HTML。
header('Content-Type:text / plain'  ); 
 
 //获取文件内容,假设文件可读(并且存在)
 $ contents = file_get_contents($ file); 
 
 //在查询中转义特殊字符
 $ pattern  = preg_quote($ searchfor,'/'); 
 
 //完成正则表达式,匹配整行
 $ pattern =“/^.*$pattern.*\$/m";
nn  
 //搜索并存储所有匹配的出现在$ matches 
if(preg_match_all($ pattern,$ contents,$ matches)){
echo“找到匹配项:
”; 
 $ result = implode(“
  “,$ matches [0]); 
echo $ result; 
 
 
} 
  else {
 echo“找不到匹配项”; 
} 
 
 
} 
?&gt; 
   
 
 

$ match是数组 ...我需要对$ matches 的每个结果进行索引

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanpa2143 2013-09-05 07:52
    已采纳

    you can use array_walk:

    function fcn(&$item) {
       $item = substr(..do what you want here ...);
    }
    
    array_walk($matches, "fcn");
    
    点赞 评论
  • douchengchen7959 2013-09-05 08:02

    Proper use of array_walk

    array_walk( $matches, substr(your area));
    

    Array_map accepts several arrays

    array_map(substr(your area),  $matches1, $origarray2);
    

    in your case

     array_map(substr(your area),  $matches);
    

    Read more:

    array_map

    array_walk

    点赞 评论
  • dongyi5425 2019-02-17 10:27

    To find a sub string in an array I use this function on a production site, works perfectly.

    I convert the array to a collection because it's easier to manage.

    public function substrInArray($substr, Array $array) {
        $substr = strtolower($substr);
        $array = collect($array); // convert array to collection
    
        return $body_types->map(function ($array_item) {
            return strtolower($array_item);
        })->filter(function ($array_item) use ($substr) {
            return substr_count($array_item, $substr);
        })->keys()->first();
    
    }
    

    This will return the key from the first match, it's just an example you can tinker. Returns null if nothing found.

    点赞 评论

相关推荐 更多相似问题