How to solve the problem that php still has blank space after using strip_tags() to remove html tags

*文
Release: 2023-03-18 20:20:01
Original
5573 people have browsed it

This article mainly introduces the solution to the problem of using strip_tags() in PHP to remove html tags that still have blank spaces. It involves related operation skills of PHP regular replacement. Friends in need can refer to it. I hope to be helpful.

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

After strip_tags() is processed, it becomes

  你好
      空行...
  alsdkjf;laskjd;flasj;
      空行...
  asdjfasjd;lkafsj;lfdkj
      空行...
  aksdjfl;ak
Copy after login

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 a blank character, \S represents a non-blank character (note the case, generally the upper and lower case means the opposite meaning)

Related recommendations:

Detailed explanation of regular expression capturing groups and non-capturing groups in PHP

Basics and simple examples of PHP regular expressions

Summary of commonly used functions in php

The above is the detailed content of How to solve the problem that php still has blank space after using strip_tags() to remove html tags. 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!