Home > Backend Development > PHP Tutorial > Solution to using strip_tags() in php to remove html tags and still have blank spaces

Solution to using strip_tags() in php to remove html tags and still have blank spaces

不言
Release: 2023-03-30 16:08:02
Original
2108 people have browsed it

This article mainly introduces the solution for PHP to use strip_tags() to remove HTML tags and still have blank spaces. It involves related operation skills of PHP regular replacement. Friends who need it can refer to it.

The example of this article tells about PHP Use strip_tags() to remove html tags and still have blank solutions. Share it with everyone for your reference, the details are as follows:

$subject = strip_tags($newsRs['content']);//去除html标签
$pattern = '/\s/';//去除空白
$content = preg_replace($pattern, '', $subject);      
$seodata['articledescription'] = mb_substr($content, 0, 80);//截取80个汉字
Copy after login

Note: strip_tags(); in the first step; just replaces the html tags with spaces

For example:

<p>
  张志斌张志斌张志斌张志斌张志斌张志
</p>
<p>
  <br />
</p>
<p>
  alsdkjf;laskjd;flasj;
</p>
<p>
  asdjfasjd;lkafsj;lfdkj
</p>
<p>
  aksdjfl;ak
</p>
Copy after login

strip_tags() will become

  张志斌张志斌张志斌张志斌张志斌张志
  
  
  
  
  alsdkjf;laskjd;flasj;
  
  
  asdjfasjd;lkafsj;lfdkj
  
  
  aksdjfl;ak
Copy after login

after processing. This may not be what we want, and then use regular replacement to remove the blanks.

The final result:

张志斌张志斌张志斌张志斌张志斌张志alsdkjf;laskjd;flasj;asdjfasjd;lkafsj;lfdkjaksdjfl;ak
Copy after login

In addition: \s represents blank characters, \S represents non-blank characters (pay attention to upper and lower case, generally upper and lower case means the opposite meaning)

The above is the entire content of this article, I hope it will be helpful to everyone's study For help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

PHP function strip_tags/htmlspecialchars

PHP strip_tags method of retaining multiple HTML tags, strip_tags tag

The above is the detailed content of Solution to using strip_tags() in php to remove html tags and still have blank spaces. For more information, please follow other related articles on the PHP Chinese website!

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