Home  >  Article  >  Backend Development  >  How to implement regular expression to remove spaces in php

How to implement regular expression to remove spaces in php

藏色散人
藏色散人Original
2021-03-02 09:46:012038browse

php regular way to remove spaces: first create a PHP sample file; then define a string with spaces; finally pass "preg_replace("/(\s|\ \;| |\xc2\xa0 )/","",$str);" can be used to remove spaces in the string.

How to implement regular expression to remove spaces in php

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

php Regularly removes e388a4556c0f65e1904146cc1a846bee 94b3e26ee717c64999d7867364b1b4a3 spaces  

$str='
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><strong><span style="font-size: 18px;">Factory Supply High Quality Maitake Mushroom Extract Powder Bulk</span></strong></p>
<p> </p>
<p> </p>Product Name
<p> </p>&#39;;
$uuu=preg_replace("/(\s|\&nbsp\;| |\xc2\xa0)/","",$str);
echo str_replace(&#39;<p></p>&#39;, &#39;&#39;, $uuu);

is perfect.

How does PHP's str_replace function replace e388a4556c0f65e1904146cc1a846bee076402276aae5dbec7f672f8f4e5cc8194b3e26ee717c64999d7867364b1b4a3?

How to replace e388a4556c0f65e1904146cc1a846bee076402276aae5dbec7f672f8f4e5cc8194b3e26ee717c64999d7867364b1b4a3 with str_replace function of php as follows

 $html="<p>fdasf</p>";
  echo $string = str_replace(array("<p>","","</p>"),"",$html);
  br<http://bbs.houdunwang.com/>
若是<p>     内容</p>替换成<p>内容</p><p>                 content</p>替换成<p>contend</p>(空格是tab键和空格键  混合的  都有可能)方法如下
  $html=preg_replace(&#39;/[\n\r\t]/&#39;,&#39;&#39;,$html);//去空格
若是<p>后面跟了若干个,再是内容 
  <p>     内容</p>
  替换成<p>内容</p>
  <p>   content</p>
  替换成<p>contend</p>
  <?php
  $html="<p>
  内容</p>替换成<p>内容</p>
  <p>content</p>替换成<p>contend</p>";方法如下
  $html=trim($html);
  $html=str_replace(PHP_EOL,"",$html);
  $html=str_replace(" ","",$html);
  $html=preg_replace(&#39;/\s+/&#39;,&#39;&#39;,$html);
  $html=preg_replace(&#39;/[\n\r\t]/&#39;,&#39;&#39;,$html);
  echo "{$html}";
  ?>
str_replace("<p><br\/><\/p>","",$htmlstr);

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of How to implement regular expression to remove spaces in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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