Home >Backend Development >PHP Tutorial >Segment Chinese and English sentences without garbled characters_PHP tutorial
Thinking:
1. Encode the string with urlencode.
2. Use regular expressions to search for sentences ending with transcoded punctuation.
3.urldecode decoded data
1
echo
urlencode(
',-.-?-!-;-,-。-?-!-;-......'
).PHP_EOL;
2
$str
=
"测试测试,
Test test.
Test test?
Test test!
Test test;
Test test,
Test test。
Test test?测试测试!测试测试;测试测试......"
;
3
echo
$str
;
4
$str
= urlencode(
$str
);
5
preg_match_all (
'/(.*?)(.{3,6}|%2C|.|%3F|%21|%3B|%EF%BC%8C|%E3%80%82|%EF%BC%9F|%EF%BC%81|%EF%BC%9B)/'
,
$str
,
$matchs
);
6
foreach
(
$matchs
[0]
as
$v
){
7
$newArr
[] = urldecode(
$v
);
8
}
9
var_dump(
$newArr
);