Home > Backend Development > PHP Tutorial > 正则表达式多余空格如何去掉

正则表达式多余空格如何去掉

WBOY
Release: 2016-06-13 12:49:31
Original
1251 people have browsed it

正则表达式多余空格怎么去掉?

<br />
echo $keyword = "上海|北京!成都 深圳";<br />
echo "<br />"; <br />
$old1 = array("/(\s*)(\s)(\s*)/","/(\s*)(\|)(\s*)/","/(\s*)(!)(\s*)/");<br />
$new1 = array('" "','"|"','" "!');<br />
echo $keyword3="\"".preg_replace($old1,$new1,$keyword)."\"";<br />
//输出:"上海"|"北京" "!成都" "深圳"<br />
<br />
echo $keyword = "上海 | 北京 !成都   深圳";<br />
echo "<br />"; <br />
$old1 = array("/(\s*)(\s)(\s*)/","/(\s*)(\|)(\s*)/","/(\s*)(!)(\s*)/");<br />
$new1 = array('" "','"|"','" "!');<br />
echo $keyword3="\"".preg_replace($old1,$new1,$keyword)."\"";<br />
//输出:"上海" ""|"" "北京" "" "!成都" "深圳"<br />
Copy after login

上面两种情况:第一种是显示是对的,第二种给各符号间加了空格输去就错误了。请问多余的空格怎么屏蔽掉?
正则表达式


------解决方案--------------------
没看懂你的需求,我做的话用\S提取就足够了
------解决方案--------------------
下面的做法能满足你的需要吗?

<br />
echo $keyword = "上海<br><font color='#FF8000'>------解决方案--------------------</font><br>北京!成都 深圳";<br />
echo "<br />"; <br />
$old1 = array("/(\s*)(\s)(\s*)/","/(\s*)(\<br><font color='#FF8000'>------解决方案--------------------</font><br>)(\s*)/","/(\s*)(!)(\s*)/");<br />
$new1 = array('" "','"<br><font color='#FF8000'>------解决方案--------------------</font><br>"','" "!');<br />
echo $keyword3="\"".preg_replace($old1,$new1,$keyword)."\"";<br />
//输出:"上海"<br><font color='#FF8000'>------解决方案--------------------</font><br>"北京" "!成都" "深圳"<br />
<br />
echo $keyword = "上海 <br><font color='#FF8000'>------解决方案--------------------</font><br> 北京 !成都   深圳";<br />
$keyword = str_replace(" ","",$keyword);<br />
echo "<br />"; <br />
$old1 = array("/(\s*)(\s)(\s*)/","/(\s*)(\<br><font color='#FF8000'>------解决方案--------------------</font><br>)(\s*)/","/(\s*)(!)(\s*)/");<br />
$new1 = array('" "','"<br><font color='#FF8000'>------解决方案--------------------</font><br>"','" "!');<br />
echo $keyword3="\"".preg_replace($old1,$new1,$keyword)."\"";<br />
//输出:"上海" ""<br><font color='#FF8000'>------解决方案--------------------</font><br>"" "北京" "" "!成都" "深圳"<br />
Copy after login

------解决方案--------------------
这样写

$keyword = "上海<br><font color="#FF8000">------解决方案--------------------</font><br>北京!成都 深圳";     //输出:"上海"<br><font color="#FF8000">------解决方案--------------------</font><br>"北京" "!成都" "深圳"<br>
$keyword = "上海 <br><font color="#FF8000">------解决方案--------------------</font><br> 北京 !成都   深圳";//输出:"上海"<br><font color="#FF8000">------解决方案--------------------</font><br>"北京" "!成都" "深圳"<br>
echo "<br>"; <br>
$old1 = array("/!?[^\s!<br><font color="#FF8000">------解决方案--------------------</font><br>]+/", "/\s+/", "/\s*\<br><font color="#FF8000">------解决方案--------------------</font><br>\s*/", '/""/');<br>
$new1 = array('"$0"', ' ', ' <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template