Home  >  Article  >  Backend Development  >  How to remove consecutive spaces in php

How to remove consecutive spaces in php

藏色散人
藏色散人Original
2022-11-03 10:16:011782browse

php method to remove consecutive spaces: 1. Create a PHP sample file; 2. Use the "strip_tags()" function to strip the HTML, XML and PHP tags in the string; 3. Use "str_replace" Use the "preg_replace" function to delete consecutive spaces.

How to remove consecutive spaces in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php How to remove consecutive spaces?

PHP removes consecutive spaces

<?php
    $note = strip_tags($value[&#39;Content&#39;]);
    $note = trim($note);
    $note = str_replace(" ","",$note);
    $note = preg_replace(&#39;#\s+#&#39;, &#39; &#39;,trim($note));
    $note = str_replace("<","<",$note);
    $note = str_replace(">",">",$note);
    $note = preg_replace("/[ ]{1,}/isu"," ",$note);
    echo getsubstr($note,100);
    ?>

Related introduction:

strip_tags() function strips HTML, XML and PHP tags in strings.

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

This function must follow the following rules:

If the searched string is an array, then it will return an array. If the string being searched 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 elements found, the excess elements will be replaced with empty strings. If you search an array and replace only one string, the replacement string will apply to all found values.

Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary safe.

Grammar

str_replace(find,replace,string,count)

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to remove consecutive spaces 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