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)".
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('content-type:text/html;charset=utf-8'); $str=" <b>hello</b> world !<br> ! "; echo $str."<br>"; $newstr=strip_tags($str); echo $newstr; ?>
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('content-type:text/html;charset=utf-8'); $str=" <b>hello</b> world !<br> ! "; echo $str."<br><br>"; $newstr=strip_tags($str); echo $newstr."<br><br>"; echo str_replace(" ", "", $newstr); ?>
Description:
str_replace() function replaces some characters in a string (case sensitive).
str_replace(find,replace,string,count)
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!