21,886
社区成员
发帖
与我相关
我的任务
分享
$str = 'abcdefghijkladgs';
preg_match_all('#a#',$str,$m,PREG_OFFSET_CAPTURE);
$pos = 2;
print_r($m[0][$pos-1][1]);
function check($s, $s1, $n){
$s = '@'.$s;
$j = $x = $y = 0;
for($i = 0; $i < strlen($s); $i++){
if($index = strpos($s, $s1, $y? ($y + 1):$y)){
$j++;
if($j == $n){
$x = $index;
break;
}else{
$y = $index;
}
}
}
return $x - 1;
}
$s = 'afsdfhsafdfkljsldfj';
$s1 = 'f';
echo check($s, $s1, 2);
$str = 'abcdefghijkladgsabcdefghijkladgs';
function getPos($str, $sonStr, $iIndex=0) {
static $aPos = array();
$ipos = false;
$ipos = strpos($str, $sonStr, $iIndex);
if ($ipos !== false) {
$aPos[count($aPos)] = $ipos;
getPos($str, $sonStr, $ipos+1);
}
return $aPos;
}
print_r(getPos($str, 'a'));
out:
Array
(
[0] => 0
[1] => 12
[2] => 16
[3] => 28
)
function check($s, $s1, $n){
$j = 0;
$x = 0;
$y = 0;
for($i = 0; $i < strlen($s); $i++){
if($index = strpos($s, $s1, $y + 1)){
$j++;
if($j == $n){
$x = $index;
break;
}else{
$y = $index;
}
}
}
return $x;
}
$s = 'afsdfhsafdfkljsldfj';
$s1 = 'f';
echo check($s, $s1, 2);
$str = 'abcdefghijkladgs';
echo strpos($str,'a',2);