strpos (PHP 3, PHP 4 )strpos -- Find position of first occurrence of a string Description int strpos ( string haystack, string needle [, int offset]) Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos(), this function can take a full string as the needle parameter and the entire string will be used. If needle is not found, returns FALSE.
$str1 = '123';
$str2 = '456';
$str3 = '789';//情况1 $str1和$str3之间没有$str2
//$mystring = 'aa'.$str1.'bb'.$str2.'cc'.$str1.'dd'.$str3.'ee'.$str1.'ff'.$str2;//情况2 $str1和$str3之间有一个$str2
$mystring = 'aa'.$str1.'bb'.$str2.'cc'.$str1.'dd'.$str2.'hh'.$str3.'ee'.$str1.'ff'.$str2;//情况3 $str1和$str3之间有两个$str2
//$mystring = 'aa'.$str1.'bb'.$str2.'cc'.$str1.'dd'.$str2.'hh'.$str2.'hh'.$str3.'ee'.$str1.'ff'.$str2;echo "串1:".$str1."<br>";
echo "串2:".$str2."<br>";
echo "串3:".$str3."<br>";
echo "原串:".$mystring."<br>";//$str3只出现一次,找它位置
$str3pos = strpos($mystring,$str3);//切割字符串,从开头到$str3出现的位置
$mystring=substr($mystring,0,$str3pos);
$length=strlen($mystring);//切割字符串,从str1最后出现到结束.
$substring = strrchr($mystring,$str1);
$sublength=strlen($substring);if ($sublength>0){
//计算切割后字符串中str2的数量,
$str2nums=substr_count($substring,$str2);
if ($str2nums==1){
echo "位置为:".($length-$sublength);
}else{
echo "字符串不合要求";
}
}
?>
(PHP 3, PHP 4 )strpos -- Find position of first occurrence of a string
Description
int strpos ( string haystack, string needle [, int offset])
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos(), this function can take a full string as the needle parameter and the entire string will be used. If needle is not found, returns FALSE.