How to correctly replace multiple white spaces with a single white space in PHP?

徘徊边缘 提交于 2020-06-15 05:52:09

问题


I was scouring through SO answers and found that the solution that most gave for replacing multiple spaces is:

$new_str = preg_replace("/\s+/", " ", $str);

But in many cases the white space characters include UTF characters that include line feed, form feed, carriage return, non-breaking space, etc. This wiki describes that UTF defines twenty-five characters defined as whitespace.

So how do we replace all these characters as well using regular expressions?


回答1:


When passing u modifier, \s becomes Unicode-aware. So, a simple solution is to use

$new_str = preg_replace("/\s+/u", " ", $str);
                             ^^

See the PHP online demo.




回答2:


The first thing to do is to read this explanation of how unicode can be treated in regex. Coming specifically to PHP, we need to first of all include the PCRE modifier 'u' for the engine to recognize UTF characters. So this would be:

$pattern = "/<our-pattern-here>/u";

The next thing is to note that in PHP unicode characters have the pattern \x{00A0} where 00A0 is hex representation for non-breaking space. So if we want to replace consecutive non-breaking spaces with a single space we would have:

$pattern = "/\x{00A0}+/u";
$new_str = preg_replace($pattern," ",$str);

And if we were to include other types of spaces mentioned in the wiki like:

  • \x{000D} carriage return
  • \x{000C} form feed
  • \x{0085} next line

Our pattern becomes:

$pattern = "/[\x{00A0}\x{000D}\x{000C}\x{0085}]+/u";

But this is really not great since the regex engine will take forever to find out all combinations of these characters. This is because the characters are included in square brackets [ ] and we have a + for one or more occurrences.

A better way to then get faster results is by replacing all occurrences of each of these characters by a normal space first. And then replacing multiple spaces with a single normal space. We remove the [ ]+ and instead separate the characters with the or operator | :

$pattern = "/\x{00A0}|\x{000D}|\x{000C}|\x{0085}/u";
$new_str = preg_replace($pattern," ",$str); // we have one-to-one replacement of character by a normal space, so 5 unicode chars give 5 normal spaces
$final_str = preg_replace("/\s+/", " ", $new_str); // multiple normal spaces now become single normal space



回答3:


A pattern that matches all Unicode whitespaces is [\pZ\pC]. Here is a unit test to prove it.

If you're parsing user input in UTF-8 and need to normalize it, it's important to base your match on that list. So to answer your question that would be:

$new_str = preg_replace("/[\pZ\pC]+/u", " ", $str);



来源:https://stackoverflow.com/questions/40264465/how-to-correctly-replace-multiple-white-spaces-with-a-single-white-space-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!