清理带有标点符号错误和缺失空格的字符串
Cleaning up a string with punctuation mistakes and missing spaces
您好,我有以下字符串:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。1非常悲伤的一天!今天也是素食节。我们怎样才能帮助他?也许用小鹿斑比和一首好歌!或者搭配面包和黄油;你 think:agree?
我需要这个:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。 1 非常悲伤的一天!也是素食节。我们可以怎样帮助他?也许与小鹿斑比和一首好歌!或者搭配面包和黄油;你怎么看:同意?
1very 和 1Very 应该以同样的方式对待。
我试过这个:
<?php
$string="TheLion is walking(proudly) through theJungle,but he misses hisTeddy.1very sad day!It is VegeterianDay,too. How can we help him?Maybe withBambi&a good song! Or with bread & butter;What do you think:agree?";
echo trim(preg_replace_callback('~\b\'\b(*SKIP)(*F)|\s*(\p{P}+)\s*~u', function($m) {
return preg_replace('~\X(?=\X)~u', '[=10=] ', $m[1]) . ' ';
}, $string));
?>
结果:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。 1非常悲伤的一天!这也是素食节。我们可以怎样帮助他?也许与小鹿斑比&一首好歌!或者搭配面包和黄油;你怎么看:同意?
非常感谢
感谢@Thefourthbird
<?php
$str = "TheLion is walking(proudly) through theJungle,but he misses hisTeddy.1very sad day!It is VegeterianDay,too. How can we help him?Maybe withBambi&a good song! Or with bread & butter;What do you think:agree?";
$re = '/\b(?=[(][A-Za-z])|(?<=[,.!;:?)])\b|(?<=[a-z])(?=[A-Z])|(?<=[a-z]&)|(?=&[a-z])|(?<=[0-9])(?=[a-zA-Z])/m';
$subst = ' ';
$result = preg_replace($re, $subst, $str);
echo $result;
?>
结果:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。 1 非常悲伤的一天!也是素食节。我们可以怎样帮助他?也许与小鹿斑比和一首好歌!或者搭配面包和黄油;你怎么看:同意?
周末愉快!
您好,我有以下字符串:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。1非常悲伤的一天!今天也是素食节。我们怎样才能帮助他?也许用小鹿斑比和一首好歌!或者搭配面包和黄油;你 think:agree?
我需要这个:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。 1 非常悲伤的一天!也是素食节。我们可以怎样帮助他?也许与小鹿斑比和一首好歌!或者搭配面包和黄油;你怎么看:同意?
1very 和 1Very 应该以同样的方式对待。
我试过这个:
<?php
$string="TheLion is walking(proudly) through theJungle,but he misses hisTeddy.1very sad day!It is VegeterianDay,too. How can we help him?Maybe withBambi&a good song! Or with bread & butter;What do you think:agree?";
echo trim(preg_replace_callback('~\b\'\b(*SKIP)(*F)|\s*(\p{P}+)\s*~u', function($m) {
return preg_replace('~\X(?=\X)~u', '[=10=] ', $m[1]) . ' ';
}, $string));
?>
结果:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。 1非常悲伤的一天!这也是素食节。我们可以怎样帮助他?也许与小鹿斑比&一首好歌!或者搭配面包和黄油;你怎么看:同意?
非常感谢
感谢@Thefourthbird
<?php
$str = "TheLion is walking(proudly) through theJungle,but he misses hisTeddy.1very sad day!It is VegeterianDay,too. How can we help him?Maybe withBambi&a good song! Or with bread & butter;What do you think:agree?";
$re = '/\b(?=[(][A-Za-z])|(?<=[,.!;:?)])\b|(?<=[a-z])(?=[A-Z])|(?<=[a-z]&)|(?=&[a-z])|(?<=[0-9])(?=[a-zA-Z])/m';
$subst = ' ';
$result = preg_replace($re, $subst, $str);
echo $result;
?>
结果:
狮子正(自豪地)穿过丛林,但他想念他的泰迪熊。 1 非常悲伤的一天!也是素食节。我们可以怎样帮助他?也许与小鹿斑比和一首好歌!或者搭配面包和黄油;你怎么看:同意?
周末愉快!