Q:

How to replace repetitive punctuation in PHP using regex?

Let say, I have this
Hello ??? WHERE ARE YOU!!!! Comon ?!?!?!
desired outout
Hello ? WHERE ARE YOU!!!! Comon ?!

How can I achieve this? I tried preg_replace_callback but to no luck.  I used Finding the shortest repetitive pattern in a string as an starting point but it works on complete sentence, I need it to work on word by word + I need to remove duplicate computations only (patterns)? Live Code

A:

Use below code:
$str = "Hello ??? WHERE ARE YOU!!!! Comon ?!?!?! ...";

$replacement = [
    '?',
    '?!',
    '.',
];

foreach( $replacement as $key => $value ){
    $pattern[$key] = sprintf("/(\%s)+/", $value);
}

echo $outout = preg_replace($pattern, $replacement, $str);

Insert any punctuation to replacement array to delete repetitive punctuation.

