How to remove spaces in php using regular expressions

藏色散人
Release: 2023-03-04 15:44:02
Original
3735 people have browsed it

php method to use regular expressions to remove spaces: first create a PHP sample file; then use the regular expression "preg_replace('/[\n\r\t]/','',$html);" Just remove the spaces.

How to remove spaces in php using regular expressions

Recommended: "PHP Video Tutorial"

##php Use regular rules to remove spaces

php’s str_replace function replaces


The method is 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>
Copy after login

 <?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);
Copy after login

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!