search
Homephp教程PHP源码php去除字符串首尾中英文空格程序

下面本文章来给各位同学总结了几种php去除字符串首尾中英文空格程序实例,这里有用正则替换与trim系列函数删除,下面我们来看看。

<script>ec(2);</script>

例1.trim函数删除空格


trim()函数用于去除字符串开始位置以及结束位置的空格,并返回去掉空格后的字符串。语法如下:
string trim(string str[,string charlist]);
ltrim()函数用于去除字符串左边的空格或指定字符串。语法如下:
string ltrim(string str[,string charlist]);
rtrim()函数用于去除字符串右边的空格和特殊字符。语法如下:
string rtrim(string str[,string charlist]);

 代码如下 复制代码
 
$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
 
?>

 
输出结果:
(a,b,c,)
a,b,c,
a,b,c
在sql中函数trim()用途为去除首尾空格,ltrim()为去除字符串左侧空格,rtrim()为去除字符串右侧空格。

 

例2.利用str_replace正则替换

 代码如下 复制代码

function  mbTrim($str) 

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

例3.str_replace正则替换

 代码如下 复制代码


$str="      www.111cn.net     ";
$str = mb_ereg_replace('^( | )+', '', $str);
$str = mb_ereg_replace('( | )+$', '', $str);
echo mb_ereg_replace('  ', "n  ", $str);
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools