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));
}