Home >php教程 >PHP源码 >PHP采集利器:根据开始字符串和结束字符串截取需要的采集内容数据,纯PHP函数高性能实现,支持多种字符编码格式

PHP采集利器:根据开始字符串和结束字符串截取需要的采集内容数据,纯PHP函数高性能实现,支持多种字符编码格式

PHP中文网
PHP中文网Original
2016-05-26 08:21:03978browse

php代码

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