Home  >  Article  >  Backend Development  >  How to intercept a string between two specified characters in php

How to intercept a string between two specified characters in php

小云云
小云云Original
2018-03-16 14:15:3810148browse

This article mainly shares with you how to intercept the string between two specified characters in PHP. I hope it can help you.

/**
 * 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);
}
调用
echo $this->cut('token/','?code',$redirectUrl);die;

The above method is not very easy to use. When the intercepted value is a string, there will be a situation where the interception cannot be intercepted. Use the following method

/*
 * php截取指定两个字符之间字符串
 * */
function get_between($input, $start, $end) {
    $substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));
    return $substr;
}
调用
$sVid = $this->get_between($redirectUrl, "token/", "?code=");

Related recommendations:

Instances of php intercepting Chinese strings and getting the number of characters in Chinese strings

A PHP class that intercepts strings between specified strings

Solving the problem of intercepting Chinese strings with PHP

The above is the detailed content of How to intercept a string between two specified characters in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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