Home  >  Article  >  Backend Development  >  How to remove full-width spaces in php

How to remove full-width spaces in php

藏色散人
藏色散人Original
2021-06-21 09:14:132564browse

How to remove full-width spaces in php: First create a PHP sample file; then use the "function myTrim($str){...}" method to remove full-width spaces.

How to remove full-width spaces in php

## The operating environment of this article: Windows 7 system, PHP version 7.1 , Dell G3 computer.

How to remove full-width spaces in php?

php common methods for removing empty characters and spaces, full-width spaces, line breaks, etc. in strings

    • Function to remove spaces from strings
    • Filter spaces, full-width spaces, line breaks
    • Use regular preg_replace function

Function to remove string spaces

trim() – Removes null characters from both ends of the string

ltrim() – Removes null characters from the front of the string
rtrim() – Removes null characters from the end of the string
chop() – Same as rtrim( ).

<?php
$text = " www.qipa250.com ";
$leftTrimmed = ltrim($text);
$rightTrimmed = rtrim($text);
$bothTrimmed = trim($text);
print("leftTrimmed = ($leftTrimmed)");
echo "<hr>";
print("rightTrimmed = ($rightTrimmed)");
echo "<hr>";
print("bothTrimmed = ($bothTrimmed)");
echo "<hr>";
?>

Output results


How to remove full-width spaces in php

Filter spaces, full-width spaces, line breaks

function myTrim($str)
{
    $search = array(" "," ","\n","\r","\t");
    $replace = array("","","","","");
    return str_replace($search, $replace, $str);
}

echo &#39;当前字符串内容:&#39;.$str = &#39;www.qipa250.com
QIPA250 奇葩天地网 qipa250.com&#39;;

echo "<hr>";
echo &#39;过滤后的字符串内容:&#39;.myTrim($str);

echo "<hr>";

The results are as follows:


How to remove full-width spaces in php

Use the regular preg_replace function

echo preg_replace(&#39;# #&#39;, &#39;&#39;, &#39;ab     ab&#39;);
//输出 "abab"

Recommended study: "

PHP Video Tutorial"                

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