简体   繁体   English

PHP Regex去除多余的标点符号

[英]PHP Regex to strip excess punctuation

So basically we need a regex to strip excess punctuation from a string, leaving only one of the punctuation characters. 因此,基本上我们需要一个正则表达式来从字符串中去除多余的标点符号,仅保留其中一个标点符号。

So: 所以:

This is my awesome string!!!!! 这是我很棒的弦! Don't you love it???!!??!! 你不喜欢它吗?

Would result in 会导致

This is my awesome string! 这是我很棒的弦! Don't you love it?! 你不喜欢吗?

I have tried and tried and tried to get this, but I either end up mangling the string or it doesn't work at all. 我已经尝试了很多次,但是最终要么破坏了字符串,要么根本不起作用。 I'm still learning Regexes so please forgive what is surely a stupid question. 我仍在学习正则表达式,因此请原谅确实是一个愚蠢的问题。

I guess "punctuation" would be pretty much anything that's not A-Za-z0-9 我猜“标点符号”几乎是不是A-Za-z0-9的任何东西

Edit It appears that I misunderstood our original requirements. 编辑看来我误解了我们的原始要求。 Using the accepted solution below, how would I adjust it so that no matter what characters you have, the punctuation is limited to the first only? 使用下面接受的解决方案,我将如何调整它,以便无论您使用什么字符,标点符号都仅限于第一个?

IE IE浏览器

???!!!!!! ??? !!!!!!

would become just 会变得公正

?

And

This is my string!!!?!?!?!? 这是我的弦!!!!!?!?!? Isn't it great???!?!?!! 很棒吗???!?!!?!!

would become 会成为

This is my string! 这是我的弦! Isn't it great? 很好吗?

Similar to the other answers, but should take care of any non 0-9a-zA-Z characters in any order leaving you with one of each left: 与其他答案类似,但应以任何顺序处理任何非0-9a-zA-Z字符,使您各剩下一个:

$newstring= preg_replace('/([^\w\s])(?=[^\w\s]*\1)/', '', $oldstring);

Should turn 应该转

This is my awesome string!!!!! Don't you love it???!!??!!

into 进入

This is my awesome string! Don't you love it?!

It works by using a positive lookahead to see if the character appears again in this string of punctuation. 它通过使用积极的前瞻来查看字符是否再次出现在此标点符号字符串中。 If it does, it's replaced with the empty string. 如果是这样,则将其替换为空字符串。

Something like preg_replace('#([!?])\\1+#', '$1') , perhaps? preg_replace('#([!?])\\1+#', '$1')吗? For example: 例如:

$t = 'This is my awesome string!!!!! Don\'t you love it???!!??!!';
$u = preg_replace('#([!?])\1+#', '$1', $t);

// to clear out all these '?!?!...' and '!?!?...' sequences.
$u = preg_replace('#(\?!|!\?)\1+#', '$1', $u); 


echo $u; // This is my awesome string! Don't you love it?!

Try preg_replace_callback : 尝试preg_replace_callback

preg_replace_callback('/[!?]+/', function($m) {
    $excl = strpos($m[0], '!');
    $ques = strpos($m[0], '?');

    if($excl !== false && $excl <= $ques) {
        return $ques === false ? '!' : '!?';
    } else {
        return $excl === false ? '?' : '?!';
    }
}, $str);

Here's a demo. 这是一个演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM