首页
IT
登录
6mi
u
盘
搜
搜 索
IT
php格式化文本(换行)
php格式化文本(换行)
xiaoxiao
2021-03-26
34
function
TxtFormat
(
$txt
)
{
if
((stripos(
$txt
,
"\n"
) >
0
)) {
return
$txt
; }
$tags
= [
'!'
,
'!'
,
'。'
,
'?'
,
'?'
];
$jishu
=
0
;
$lianxu
=
30
;
$insertBr
=
"\r\n\r\n "
;
$isFlag
=
false
;
$lastchar
=
''
;
$tempChar
=
''
;
$sb
=
" "
;
$txt_count
= mb_strlen(
$txt
,mb_detect_encoding(
$txt
,
array
(
'GB2312'
,
'GBK'
,
'UTF-8'
)));
for
(
$i
=
0
;
$i
<
$txt_count
;
$i
++) {
$tempChar
= mb_substr(
$txt
,
$i
,
1
,
'utf-8'
);
if
(
$tempChar
==
'\r'
||
$tempChar
==
'\n'
||
$tempChar
==
' '
||
$tempChar
==
' '
) {
continue
; } ++
$jishu
;
if
(
$tempChar
==
'“'
) {
$isFlag
=
true
;
$sb_count
= mb_strlen(
$sb
,
'UTF-8'
);
if
(
0
<
$sb_count
) {
$lastchar
= mb_substr(
$sb
,
$sb_count
-
1
,
1
,
'utf-8'
); }
else
{
$lastchar
=
'0'
; }
if
(
$lastchar
!=
'0'
&& ((
$lastchar
!=
' '
&&
$lastchar
!=
':'
&&
$lastchar
!=
','
&&
$lastchar
!=
','
) ||
$lastchar
==
'”'
)) {
$jishu
=
0
;
$sb
.=
$insertBr
; }
$sb
.=
$tempChar
;
continue
; }
if
(
$isFlag
&&
$tempChar
==
'”'
) {
$isFlag
= !
$isFlag
;
$sb
.=
$tempChar
;
continue
; }
$sb
.=
$tempChar
;
foreach
(
$tags
as
$tag
) {
if
(
$tempChar
==
$tag
) {
if
((
$isFlag
==
false
&&
$jishu
>
$lianxu
) &&
$i
+
1
<
$txt_count
&& mb_substr(
$txt
,
$i
+
1
,
1
,
'utf-8'
) !=
'”'
) {
$sb
.=
$insertBr
;
$jishu
=
0
;
$isFlag
=
false
;
break
; } } } }
return
$sb
; }
转载请注明原文地址: https://ju.6miu.com/read-662794.html
技术
最新回复
(
0
)