PHP program to remove Chinese and English spaces at the beginning and end of a string_PHP tutorial

WBOY
Release: 2016-07-13 10:48:53
Original
877 people have browsed it

The following article summarizes several PHP program examples for removing Chinese and English spaces at the beginning and end of strings. Here, regular replacement and trim series functions are used to delete them. Let’s take a look.

Example 1.trim function removes spaces


The trim() function is used to remove spaces at the beginning and end of a string, and returns the string with the spaces removed. The syntax is as follows:
string trim(string str[,string charlist]);
The ltrim() function is used to remove spaces on the left side of a string or specify a string. The syntax is as follows:
string ltrim(string str[,string charlist]);
The rtrim() function is used to remove spaces and special characters on the right side of a string. The syntax is as follows:
string rtrim(string str[,string charlist]);

a,b,c In SQL, the function trim() is used to remove leading and trailing spaces, ltrim() is to remove spaces on the left side of a string, and rtrim() is used to remove spaces on the right side of a string.
The code is as follows
 代码如下 复制代码

$a="(a,b,c,)";
echo $a."
"; //输出:(a,b,c,)
$b=trim($a,"()"); //去除字符串首尾含有的字符“(”或“)”
echo $b."
"; //输出:a,b,c,
$c=trim($a,"(,)"); //去除字符串首尾含有的字符“(”、“,”或“)”
echo $c."
"; //输出:a,b,c
 
?>
Copy code



$a="(a,b,c,)";
echo $a."
"; //Output: (a,b,c,)
$b=trim($a,"()"); //Remove the characters "(" or ")" at the beginning and end of the string echo $b."
"; //Output: a,b,c,

$c=trim($a,"(,)"); //Remove the characters "(", "," or ")" at the beginning and end of the string

echo $c."
"; //Output: a,b,c

?>

 代码如下 复制代码

function  mbTrim($str) 

        return mb_ereg_replace('(^( | )+|( | )+$)', '', $str); 

Output result:

(a,b,c,)
 代码如下 复制代码

$str="      www.bKjia.c0m     ";
$str = mb_ereg_replace('^( | )+', '', $str);
$str = mb_ereg_replace('( | )+$', '', $str);
echo mb_ereg_replace('  ', "n  ", $str);
?>

a,b,c,

The code is as follows Copy code
function mbTrim($str) {             return mb_ereg_replace('(^( | )+|( | )+$)', '', $str); }
Example 3.str_replace regular replacement
The code is as follows Copy code
$str="   www.bKjia.c0m  "; <🎜> $str = mb_ereg_replace('^( | )+', '', $str); <🎜> $str = mb_ereg_replace('( | )+$', '', $str); <🎜> echo mb_ereg_replace(' ', "n ", $str); <🎜> ?> http://www.bkjia.com/PHPjc/632753.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632753.htmlTechArticleThe following article summarizes several PHP program examples for removing Chinese and English spaces at the beginning and end of strings. It is useful here. Let’s take a look at regular replacement and trim series function deletion. For example...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!