常用的php函数
strstr(string,string) //从前面第一次出现某个字符串的地方截取到最后
strrchr(string,string) //从某个字符串从最后出现的位置截取到结尾
strpos(string,string[,int]) //某个字符串第一次出现的位置
strrpos(string,string) //某个字符串最后一次出现的位置
substr(string,int[,int]) //从指定位置开始截取字符串,可以指定截取的长度。
strlen(string) //获取字符串的长度PHP截取开始和结束标记间的字符
方法一:利用explode对字符串进行分
代码如下 |
复制代码 |
/**
* Get the content between $start and $end
*
* @param string $content 原始字符
* @param string $start 起始字符
* @param string $end 结束字符
* @return string
*/
function GetStringBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
|
方法二:利用substr截取字符,由于php组件函数,因此字符串处理效率比explode方法要高一些
代码如下 |
复制代码 |
/**
* Get the content between $start and $end
*
* @param string $content 原始字符
* @param string $start 起始字符
* @param string $end 结束字符
* @return string
*/
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
|
PHP截取所有符合起始与结束标记的字符串
代码如下 |
复制代码 |
/**
* Get all every strings between two tags
*
* @param string $string 原始字符串
* @param string $start 起始字符串
* @param string $end 结束字符串
* @return array
*/
function get_all_strings_between($string,$start,$end)
{
//Returns an array of all values which are between two tags in a set of data
$strings = array();
$startPos = 0;
$i = 0;
//echo strlen($string)."n";
while($startPos < strlen($string) && $matched = get_string_between(substr($string,$startPos),$start,$end))
{
if ($matched == null || $matched[1] == null || $matched[1] == '') break;
$startPos = $matched[0]+$startPos+1;
array_push($strings,$matched[1]);
$i++;
}
return $strings;
}
function get_string_between($string, $start, $end){
$ini = strpos($string,$start);
if ($ini == 0) return null;
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return array($ini+$len,substr($string,$ini,$len));
}
|
php截取起始与结束标记间字符,带截取次数、是否允许重复选项
代码如下 |
复制代码 |
/**
* Get all every strings between two tags
*
* @param string $string 原始字符串
* @param string $start 起始字符串
* @param string $end 结束字符串
* @return array
*/
function get_all_strings_between($string,$start,$end)
{
//Returns an array of all values which are between two tags in a set of data
$strings = array();
$startPos = 0;
$i = 0;
//echo strlen($string)."n";
while($startPos < strlen($string) && $matched = get_string_between(substr($string,$startPos),$start,$end))
{
if ($matched == null || $matched[1] == null || $matched[1] == '') break;
$startPos = $matched[0]+$startPos+1;
array_push($strings,$matched[1]);
$i++;
}
return $strings;
}
function get_string_between($string, $start, $end){
$ini = strpos($string,$start);
if ($ini == 0) return null;
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return array($ini+$len,substr($string,$ini,$len));
}
|
截取GB2312中文字符串
代码如下 |
复制代码 |
< ?php
//截取中文字符串
function mysubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i < $strlen; $i++) {
if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
?>
|
截取utf8编码的多字节字符串
代码如下 |
复制代码 |
< ?php
//截取utf8字符串
function utf8Substr($str, $from, $len)
{
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
'$1',$str);
}
?>
|