Home >Backend Development >PHP Tutorial >How to solve the problem that php still has blank space after using strip_tags() to remove html tags
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个汉字
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>
After strip_tags() is processed, it becomes
你好 空行... alsdkjf;laskjd;flasj; 空行... asdjfasjd;lkafsj;lfdkj 空行... aksdjfl;ak
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
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!