Home >Backend Development >PHP Tutorial > 正则表达式多余空格如何去掉
正则表达式多余空格怎么去掉?
<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 />
<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 />
$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>