龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

子字符串 php获取两个字符串之间的子字符串

时间:2014-06-18 02:06来源:网络整理 作者:网络 点击:
分享到:
php获取两个字符串之间的子字符串 [代码片段(19行)]
/**
 * Returns the substring between two strings, delimiters not included
 * @param string $string Haystack
 * @param string $start Starting delimiter
 * @param string|null $end Ending delimiter, if omitted will return the rest of the string
 * @return bool|string The substring between $start and $end or false if either string is not found
 */
function substr_between($string, $start, $end=null) {
    if(($start_pos = strpos($string, $start)) !== false) {
        if($end) {
            if(($end_pos = strpos($string, $end, $start_pos + strlen($start))) !== false) {
                return substr($string, $start_pos + strlen($start), $end_pos - ($start_pos + strlen($start)));
            }
        } else {
            return substr($string, $start_pos);
        }
    }
    return false;
}
精彩图集

赞助商链接