pHP substr()方法,PHP: substr - Manual

* string substrpos(string $str, mixed $start [[, mixed $end], boolean $ignore_case])

*

* If $start is a string, substrpos will return the string from the position of the first occuring $start to $end

*

* If $end is a string, substrpos will return the string from $start to the position of the first occuring $end

*

* If the first character in (string) $start or (string) $end is '-', the last occuring string will be used.

*

* If $ignore_case is true, substrpos will not care about the case.

* If $ignore_case is false (or anything that is not (boolean) true, the function will be case sensitive.

*        Both of the above: only applies if either $start or $end are strings.

*

* echo substrpos('This is a string with 0123456789 numbers in it.', 5, '5');

*        // Prints 'is a string with 01234';

*

* echo substrpos('This is a string with 0123456789 numbers in it.', '5', 5);

*        // Prints '56789'

*

* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-string')

*        // Prints 's is a string with 0123456789 numbers in it and two '

*

* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', true)

*        // Prints 's is a string with 0123456789 numbers in it and two '

*

* echo substrpos('This is a string with 0123456789 numbers in it and two strings.', -60, '-STRING', false)

*        // Prints 's is a string with 0123456789 numbers in it and two strings.'

*

* Warnings:

*        Since $start and $end both take either a string or an integer:

*            If the character or string you are searching $str for is a number, pass it as a quoted string.

*        If $end is (integer) 0, an empty string will be returned.

*        Since this function takes negative strings ('-search_string'):

*            If the string your using in $start or $end is a '-' or begins with a '-' escape it with a '\'.

*            This only applies to the *first* character of $start or $end.

*/

// Define stripos() if not defined (PHP < 5).if (!is_callable("stripos")) {

functionstripos($str,$needle,$offset=0) {

returnstrpos(strtolower($str),strtolower($needle),$offset);

}

}

functionsubstrpos($str,$start,$end=false,$ignore_case=false) {// Use variable functionsif ($ignore_case===true) {$strpos='stripos';// stripos() is included above in case it's not defined (PHP < 5).} else {$strpos='strpos';

}// If end is false, set it to the length of $strif ($end===false) {$end=strlen($str);

}// If $start is a string do what's needed to make it an integer position for substr().if (is_string($start)) {// If $start begins with '-' start processing until there's no more matches and use the last one found.if ($start{0} =='-') {// Strip off the '-'$start=substr($start,1);$found=false;$pos=0;

while(($curr_pos=$strpos($str,$start,$pos)) !==false) {$found=true;$pos=$curr_pos+1;

}

if ($found===false) {$pos=false;

} else {$pos-=1;

}

} else {// If $start begins with '\-', strip off the '\'.if ($start{0} .$start{1} =='\-') {$start=substr($start,1);

}$pos=$strpos($str,$start);

}$start=$pos!==false?$pos:0;

}// Chop the string from $start to strlen($str).$str=substr($str,$start);// If $end is a string, do exactly what was done to $start, above.if (is_string($end)) {

if ($end{0} =='-') {$end=substr($end,1);$found=false;$pos=0;

while(($curr_pos=strpos($str,$end,$pos)) !==false) {$found=true;$pos=$curr_pos+1;

}

if ($found===false) {$pos=false;

} else {$pos-=1;

}

} else {

if ($end{0} .$end{1} =='\-') {$end=substr($end,1);

}$pos=$strpos($str,$end);

}$end=$pos!==false?$pos:strlen($str);

}// Since $str has already been chopped at $start, we can pass 0 as the new $start for substr()returnsubstr($str,0,$end);

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值