php手册里的 显示所有你查找的字符的位置,以及该字符所在的单词

来源:互联网 发布:矩阵组织结构 职能组织 编辑:程序博客网 时间:2024/05/16 11:12
//该函数的功能是显示所有你查找的字符的位置,以及该字符所在的单词
function strpos_recursive($haystack, $needle, $offset = 0, &$results = array()) {                
   $offset = strpos($haystack, $needle, $offset);
   if($offset === false) {
       return $results;            
   } else {
       $results[] = $offset;
       return strpos_recursive($haystack, $needle, ($offset + 1), $results);
   }
}


$string = 'This is some string';
$search = 'i';
$found = strpos_recursive($string, $search);


if($found) {
   foreach($found as $pos) {
       echo 'Found "'.$search.'" in string "'.$string.'" at position <b>'.$pos.'</b><br />';
   }    
} else {
   echo '"'.$search.'" not found in "'.$string.'"';
}
原创粉丝点击