Home > php教程 > PHP源码 > php 切割html字符串,自动加上未结束的html标签

php 切割html字符串,自动加上未结束的html标签

PHP中文网
Release: 2016-05-25 17:09:25
Original
1324 people have browsed it


/*截取html字符串
 * @param $s 字符串
 * @param $zi 长度
 * @param $ne 没有结束符的html标签
 */
function G_htmlcut($s,$zi,$ne=',br,hr,input,img,'){
	$s=preg_replace('/\s{2,}/',' ',$s);
	$os=preg_split('//',$s);
	preg_match_all('//',$s,$or);
	$s='';
	$tag=array();
	foreach($os as $k => $v){
		if($v!='' && $v!=' '){
			$l=strlen($v);
			for($i=0;$i 127){
					$s.=$v[$i].$v[++$i].$v[++$i];
				}else{
					$s.=$v[$i];
				}
				$zi--;
				if($zi < 1){
					break 2;	
				}
			}
		}
		preg_match(&#39;/]+)[\s>]{1}/&#39;,$or[0][$k],$t);
		$s.=$or[0][$k];
		if(strpos($ne,&#39;,&#39;.strtolower($t[1]).&#39;,&#39;)===false && $t[1]!=&#39;&#39; && $t[1]!=&#39; &#39;){
			$k=array_search(&#39;&#39;,$tag);
			if($k!==false){
				unset($tag[$k]);
			}else{
				array_unshift($tag,&#39;&#39;);
			}
		}
	}
	return $s.implode(&#39;&#39;,$tag);
}
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template