开发者

on finding and replacing values from String using Strpos, by creating a function which returns the position

I am trying to figure out a way to find the position of value from a string开发者_如何学编程 using strpos, by creating a function which returns the position. Please see the code below:

<?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();

?>

Now with echo it works but not with return.


In The Above Script it is returning the list of values from function "value()".Now we have stored all the Positions in the array and we are returning that array it self.Please See modified version of your script below.

$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 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜