Home > php教程 > php手册 > PHP采集利器:根据开始字符串和结束字符串截取需要的采集内容数

PHP采集利器:根据开始字符串和结束字符串截取需要的采集内容数

WBOY
Release: 2016-06-06 19:35:26
Original
972 people have browsed it

PHP采集利器:根据开始字符串和结束字符串截取需要的采集内容数据,纯PHP函数高性能实现,无正则,效率一流,支持单个截取或者数组形式截取,第一组截取失败,就执行第二套截取方案,以此类推。。。 支持多种字符编码格式! $str=1234567890; var_dump(strCut

PHP采集利器:根据开始字符串和结束字符串截取需要的采集内容数据,纯PHP函数高性能实现,无正则,效率一流,支持单个截取或者数组形式截取,第一组截取失败,就执行第二套截取方案,以此类推。。。

支持多种字符编码格式!

$str = '1234567890';

var_dump(strCutByStr($str, '2', '9'));//输出 345678
var_dump(strCutByStr($str, array('a', '4'), array('b', '7')));//输出 56,因为第一组匹配没有找到,就执行第二组,以此类推
function strCutByStr(&$str, $findStart, $findEnd = false, $encoding = 'utf-8'){
		if(is_array($findStart)){
			if(count($findStart) === count($findEnd)){
				foreach($findStart as $k => $v){
					if(($result = strCutByStr($str, $v, $findEnd[$k], $encoding)) !== false){
						return $result;
					}
				}
				return false;
			}else{
				return false;
			}
		}
		
		if(($start = mb_strpos($str, $findStart, 0, $encoding)) === false){
			return false;
		}
		
		$start += mb_strlen($findStart, $encoding);
		
		if($findEnd === false){
			return mb_substr($str, $start, NULL, $encoding);
		}
		
		if(($length = mb_strpos($str, $findEnd, $start, $encoding)) === false){
			return false;
		}
		
		return mb_substr($str, $start, $length - $start, $encoding);
	}
Copy after login
source:php.cn
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