Home > Backend Development > PHP Problem > How to remove tags and spaces in php

How to remove tags and spaces in php

青灯夜游
Release: 2023-03-16 13:46:01
Original
2465 people have browsed it

Removal steps: 1. Use the strip_tags() function to remove HTML, XML and PHP tags in the string. The syntax is "strip_tags (original string)", which will return a new string with the tags removed; 2. Use the str_replace() function to remove spaces. You only need to search for space characters in the new string and replace them with null characters. The syntax is "str_replace(" ", "", new string)".

How to remove tags and spaces in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

php remove tags and Steps for spaces

Step 1. Use the strip_tags() function to remove tags

The strip_tags() function can strip the string HTML, XML and PHP tags.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str=" <b>hello</b> world !<br> ! ";
echo $str."<br>";
$newstr=strip_tags($str);
echo $newstr;
?>
Copy after login

How to remove tags and spaces in php

Step 2: Use str_replace() function to remove spaces

Just use str_replace() function to search in the string space characters and replace them with null characters.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str=" <b>hello</b> world !<br> ! ";
echo $str."<br><br>";

$newstr=strip_tags($str);
echo $newstr."<br><br>";

echo str_replace(" ", "", $newstr);
?>
Copy after login

How to remove tags and spaces in php

Description:

str_replace() function replaces some characters in a string (case sensitive).

str_replace(find,replace,string,count)
Copy after login
Parameters Description
find Required . Specifies the value to look for.
replace Required. Specifies a value that replaces the value in find.
string Required. Specifies the string to be searched for.
count Optional. A variable counting the number of substitutions.

Return value: Returns a string or array with replacement value.​

This function must follow the following rules:

  • If the searched string is an array, then it will return an array.

  • If the searched string is an array, then it will find and replace each element in the array.

  • If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings .

  • If you search an array and replace only one string, the replacement string will work for all found values.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove tags and spaces in php. 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