我试图通过创建一个返回位置的函数来找到使用strpos的字符串值的位置。请参阅下面的代码:
<?php
// program to find out the position from a string
$find = 'is';
$string_length = strlen($find);
$offset = 0;
$string = 'This is a sting, and it is an example';
function value(){
global $find;
global $string_length;
global $offset;
global $string;
while ($string_position = strpos($string, $find, $offset) ){
$offset = $string_position + $string_length;
echo $string_position .'<br/>';
//return $string_position;
}
}
echo value();
?>
在上面的脚本中,它是从函数"value()"返回值列表。现在我们已经在数组中存储了所有的位置我们返回那个数组self。请看下面修改后的脚本。
$find = 'is';
$string_length = strlen($find);
$offset = 0;
$string = 'This is a sting, and it is an example';
function value(){
global $find;
global $string_length;
global $offset;
global $string;
while ($string_position = strpos($string, $find, $offset) ){
$offset = $string_position + $string_length;
//echo $string_position .'<br/>';
$strpos[] = $string_position;
}
return $strpos;
}
$posarr = value();
var_dump( $posarr );