Home  >  Article  >  Backend Development  >  How to remove spaces from html tags in php

How to remove spaces from html tags in php

PHPz
PHPzOriginal
2023-04-13 09:03:37657browse

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!

Statement:
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