How to remove spaces from html tags in php
In php, we often need to process html documents, which contain various tags. However, in some cases we may need to remove all tags and spaces from the text, such as when performing a search on the text or generating a summary. This article will introduce how to use php to remove html tags and spaces.
1. Remove html tags
You can use the strip_tags() function in php to remove html tags. This function takes a string parameter and it will remove all html and php tags in that string. Here is an example:
$html = '<h1>php去掉html标签</h1><p>使用strip_tags()函数</p>'; $plainText = strip_tags($html); echo $plainText;//输出:php去掉html标签使用strip_tags()函数
strip_tags() function can also specify the tags to retain in the second parameter:
$html = '<h1>php去掉html标签</h1><p>使用strip_tags()函数</p>'; $plainText = strip_tags($html, '<p>'); echo $plainText;//输出:<p>使用strip_tags()函数</p>
The above code retains the
tag and removes the < ;h1> tag.
2. Remove the spaces
Once we have deleted the html tag, the next step is to remove the spaces. Or use PHP's built-in function: preg_replace(), which can replace text using regular expressions. The following is an example of removing spaces:
$html = '<h1>php去掉html标签</h1> <p>使用strip_tags()函数</p>';
$plainText = strip_tags($html);
$plainText = preg_replace('/\s+/','',$plainText);
echo $plainText;//输出:php去掉html标签使用strip_tags()函数
In the above code, we first remove all html tags through the strip_tags() function, and then use the preg_replace() function to replace all spaces in the string with empty spaces string. The regular expression "/\s /" can match one or more spaces.
3. Combined use
Now, we can use strip_tags() and preg_replace() functions in combination to remove html tags and spaces:
$html = '<h1>php去掉html标签</h1> <p>使用strip_tags()函数</p>';
$plainText = strip_tags($html);
$plainText = preg_replace('/\s+/','',$plainText);
echo $plainText;//输出:php去掉html标签使用strip_tags()函数
The above code will delete all html tag, and remove all spaces, the output contains only the final result of the original text.
Summary
When processing HTML documents, we often need to remove tags and spaces. In php, we can remove html tags using strip_tags() function and replace spaces using preg_replace() function. These functions are not only easy to implement, but also perform well, so we should take advantage of them when working with HTML documents.
The above is the detailed content of How to remove spaces from html tags in php. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)

