My current php sentence sanitizer function:
function sanitize_sentence($string) {
$pats = array(
'/([.!?]\s{2}),/', # Abc. ,Def
'/\.+(,)/', # ......,
'/(!)!+/', # abc!!!!!!!!
'/\s+(,)/', # abc , def
'/([a-zA-Z])\1\1/'); # greeeeeeen
$fixed = preg_replace($pats,'$1',$string);
$fixed = preg_replace('/,(?!\s)/',', ',$fixed);
return $fixed;
}
echo sanitize_sentence('hello!!!!!!there should be a space after the exclamation mark.right???????yes.right,');
The result should be:
hello! there should be a space after the exclamation mark. right? yes. right.
So, what’s still missing is:
- an extra space should be inserted after any !?,.if there is more text at the right.
- if the last character is a comma (or any other character different than a-zA-Z0-9!?.), it should be replaced with a dot.
- if the user writes more than one question mark, it should be converted to one (?????? = ?). That is working fine for me for exclamation marks, but somehow it’s not working for the others.
Any help will be highly appreciated!
Your requirements:
An extra space should be inserted after any
!?,.if there is more text at the right.We can use another regex to do this replacement:
If the last character is a comma (or any other character different than
a-zA-Z0-9!?.), it should be replaced with a dot.You can get this with a regex, anchored at the end of the text:
If the user writes more than one question mark, it should be converted to one (?????? = ?). That is working fine for me for exclamation marks, but somehow it’s not working for the others.
It’s not working because
?is a special character in regexes, you need to escape it. Replace the appropriate entry with this:And now, the output is: