preg_replace去除标点符号,空格和占有形式的单词?

时间:2022-08-29 16:49:02
$text =
"<tab><return><nbsp>O'Neil  really  
likes his pudding's, he really really does!!!1.5"

I would like to have this if possible where '<tab>' is an actual tab \t space not literally '<tab>'

如果可能的话,我想有这个' '是一个实际的tab \ t空格而不是字面上的' '

$text = "O'Neil really likes his pudding he really really does 1.5"

removing the whites paces and replaces them with one space, the punctuation, keeping the numbers or anything after apostrophe thats not a 's'.

删除白人的步伐,并用一个空格替换它们,标点符号,保留数字或撇号之后的任何东西,而不是's'。

Thank you so much!

非常感谢!

1 个解决方案

#1


3  

$string = trim(preg_replace(array('~[:;!?]|[.,](?![0-9])|\'s~', '~\s+~'), array('', ' '), $string));

That first gets rid of 's and then replaces multiple whitespace with one space. Eventually it removes leading and trailing whitespace.

首先摆脱's然后用一个空格替换多个空格。最终它删除了前导和尾随空格。

#1


3  

$string = trim(preg_replace(array('~[:;!?]|[.,](?![0-9])|\'s~', '~\s+~'), array('', ' '), $string));

That first gets rid of 's and then replaces multiple whitespace with one space. Eventually it removes leading and trailing whitespace.

首先摆脱's然后用一个空格替换多个空格。最终它删除了前导和尾随空格。