search
Homephp教程php手册混合编码的文本段落重排

混合编码的文本段落重排

Jun 06, 2016 pm 07:34 PM
httptextparagraphcodingrearrange

http://www.sgcha.cn/cha.php 在文本处理中,尤其是大段的文本处理,由于html语言的规则以及文本编码的不同,使得文字容易出现乱码。同时由于换行的不同,使得文本要经常进行段落重排。 段落重排的标识主要是根据特定的标点符号来进行。具体参考代码中的注释

http://www.sgcha.cn/cha.php
在文本处理中,尤其是大段的文本处理,由于html语言的规则以及文本编码的不同,使得文字容易出现乱码。同时由于换行的不同,使得文本要经常进行段落重排。
段落重排的标识主要是根据特定的标点符号来进行。具体参考代码中的注释
$strtest = '这个是第一个
句子,
的第一部分。
的反对法 的飞洒?
\u3434,
';
$strtest =cut_str_by_mb ($strtest );

echo "<pre class="brush:php;toolbar:false">";
echo $strtest;
/*********************************************************************
输入一个字串
此处的关键是段落的标点,是一个utf-8的编码  
返回排版后的字串。

*********************************************************************/
function cut_str_by_mb ($str,$arr_tag=NULL){
    if($arr_tag==NULL){
		$arr_tag=array(
					'\u2026',
					'\u201d',
				  	'\u302',
					'\uff1f',
				  	':',
					'\uff1a',
				  );
	}
	$str=set_char_set($str);//不管先检查字符格式,转化成utf-8的再说
	$str=unescape($str);//把里面16进制编码的转化成utf-8的格式
	$tmp_array=preg_split("/((\r(?!\n))|((?<!\r)\n)|(\r\n))/", $str);//根据换行符拆分成数组
	$tmp_val='';
	foreach($tmp_array as  $v){
		if(!empty($tmp_val)){
			$v=trim($v);//去掉字符的首尾空格
		}
		$tmp_val=$tmp_val.$v;//链接后面的值,组成新的字串
		$len=mb_strlen( $tmp_val, 'utf-8') ;
		$endtag=mb_substr($tmp_val,$len-1,1,'utf-8');
		$u_tag=unicode_encode($endtag);
		if (in_array($u_tag, $arr_tag)) {
			$return_arr[]=$tmp_val;
			$tmp_val='';
		}
	} 
	$return_str=implode("\r\n",$return_arr);
    return $return_str;
}

/****************************************************************
	检查编码,统一用utf-8
**********************************************************************/
	function set_char_set($data){
	  if( !empty($data) ){
		$fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;
		if( $fileType != 'UTF-8'){
		  $data = mb_convert_encoding($data ,'utf-8' , $fileType);
		}
	  }
	  return $data;
	}
/****************************************************************
	把其中的&# 以及joson格式转化成中文
**********************************************************************/
function unescape($str) {
    $str = rawurldecode($str);
    preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);
    $ar = $r[0];
//    print_r($ar);
    foreach($ar as $k=>$v) {
        if(substr($v,0,2) == "%u"){
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,-4)));
  }
        elseif(substr($v,0,3) == "&#x"){
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,3,-1)));
  }
        elseif(substr($v,0,2) == "&#") {
             
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1)));
        }
    }
    return join("",$ar);
}	

/****************************************************************
	把utf-8编码的字符返回 unicode的字串
**********************************************************************/
function unicode_encode($name){
    $name = iconv('UTF-8', 'UCS-2', $name);
    $len = strlen($name);
    $str = '';
    for ($i = 0; $i < $len - 1; $i = $i + 2){
        $c = $name[$i];
        $c2 = $name[$i + 1];
        if (ord($c) > 0){    
			// 两个字节的文字
            $str .= '\u'.base_convert(ord($c), 10, 16).base_convert(ord($c2), 10, 16);
        }else{
            $str .= $c2;
        }
    }
    return $str;
}
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.