Home > php教程 > php手册 > php如何截取指定字符之间字符串(附代码)

php如何截取指定字符之间字符串(附代码)

WBOY
Release: 2018-10-27 14:37:34
forward
2215 people have browsed it

这篇文章主要介绍了php如何截取指定字符之间字符串,有一定的参考价值,感兴趣的朋友可以看看,希望对你有所帮助!

/**
 * php截取指定两个字符之间字符串,默认字符集为utf-8 Power by 大耳朵图图
 * @param string $begin  开始字符串
 * @param string $end    结束字符串
 * @param string $str    需要截取的字符串
 * @return string
 */
function cut($begin,$end,$str){
    $b = mb_strpos($str,$begin) + mb_strlen($begin);
    $e = mb_strpos($str,$end) - $b;

    return mb_substr($str,$b,$e);
}
Copy after login

调用

echo $this->cut('token/','?code',$redirectUrl);die;

上面的方法不是很好用,当截取的是值为串的时候,会出现截取不到的情况用下面的吧

/*  * php截取指定两个字符之间字符串  * */

function get_between($input, $start, $end) {
$substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));  
return $substr; 
 }
Copy after login

调用

$sVid = $this->get_between($redirectUrl, "token/", "?code=");

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template