8.3.1 去除空格和字符串填补及大小写转换函数

8.3.1 去除空格和字符串填补及大小写转换函数

去除空格和字符串填充补函数

函数:ltrim( )
语法: string ltrim(string str[, string charlist]);
返回值: 字符串
本函数用来删去字符串中的前导空格 (whitespace)。
函数:rtrim( ) 还有个别名:chop( )
语法: string rtrim(string str[, string charlist]);
返回值: 字符串
本函数用来删去字符串中的后缀空格 (whitespace)。
函数:trim( )
语法: string trim(string str[, string charlist]);
返回值: 字符串
截去字符串首尾的空格。本函数返回字符串 string 首尾的空白字符去除后的字串。

<?php
    //声明一个字符串,其中左侧有三个空格,右侧两个空格,总长度为9个字符
    $str = "   lamp  ";   
    
    echo strlen( $str );                //输出字符串的总长度 9
    echo strlen( ltrim($str) );       //去掉左侧空格后的长度输出为 6
    echo strlen( rtrim($str) );       //去掉右侧空格后的长度输出为 7
    echo strlen( trim($str) );        //去掉两侧空格后的长度输出为 4

    //声明一个测试字符串,左侧为数字开头,右侧为省略号“…” 
    $str = "123 This is a test ...";  
    echo ltrim($str, "0..9");         //过滤掉字符串左侧的数字,输出:This is a test ...
    echo rtrim($str, ".");              //过滤掉字符串右侧所有“.”,输出:123 This is a test
    
    //过滤掉字符串两端的数字和大写字母还有“.”,输出:his is a test
    echo trim($str, "0..9 A..Z .");   

函数:str_pad() 按需求对字符串进行填充。
语法:string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]] )
STR_PAD_LEFT 字符串左添补
STR_PAD_RIGHT 字符串右添补
STR_PAD_BOTH 字符串两端添补

<?php
    $str = "LAMP";                           
    echo str_pad($str, 10);                //指定长度为10,默认使用空格在右边填补"LAMP“
    //指定长度为10,指定在左边填补" -=-=-=LAMP"
    echo str_pad($str, 10, "-=", STR_PAD_LEFT); 
    //指定长度为10,指定两端填补 " _ _ _LAMP_ _ _“
    echo str_pad($str, 10, "_", STR_PAD_BOTH);
    echo str_pad($str, 6 , "_ _ _");    //指定长度为6, 默认在右边填补" LAMP_ _"

字符串大小写的转换

函数: strtolower( )
语法: string strtolower(string str);
本函数将字符串 str 全部变小写字符串。
函数: strtoupper( )
语法: string strtoupper(string str);
本函数将字符串 str 全部变大写字符串。
函数:ucfirst( )
将字符串第一个字符改大写。
语法: string ucfirst(string str);
本函数返回字符串 str 第一个字的字首字母改成大写。
函数:ucwords( )
将字符串每个字第一个字母改大写。
语法: string ucwords(string str);
本函数返回字符串 str 每个字的字首字母全都改成大写。

<?php
    $lamp = "lamp is composed of Linux、Apache、MySQL and PHP";
    
    echo strtolower( $lamp );  
    //输出:lamp is composed of linux、apache、mysql and php 
    
    echo strtoupper( $lamp );   
    //输出:LAMP IS COMPOSED OF LINUX、APACHE、MYSQL AND PHP 
    
    echo ucfirst( $lamp );      
    //输出:Lamp is composed of Linux、Apache、MySQL and PHP
    
    echo ucwords( $lamp ); 
    //输出:Lamp Is Composed Of Linux、Apache、MySQL And PHP 
    
    $lamp = "lamp is composed of Linux、Apache、MySQL and PHP";
    echo ucfirst( strtolower($lamp) );  
    //输出: Lamp is composed of linux、apache、mysql and php

2.php

<?php

    $str = "    hello Mworld8i9oio   ";

    echo $str. "---".strlen($str)."<br>";

    $nstr =  trim($str, "0..9a..z ");

    echo $nstr."---".strlen($nstr)."<br>";

3.php

<?php

    $str = "lamp";

    echo $str. "---".strlen($str)."<br>";

    $nstr =  str_pad($str, 10, "", STR_PAD_BOTH);

    echo $nstr."---".strlen($nstr)."<br>";

test.php

<?php
    $str = "this is a test Apache MySQL PHP";
    echo $str."<br>";
    echo strtoupper($str)."<br>";
    echo strtolower($str)."<br>";
    echo ucfirst($str)."<br>";
    echo ucwords($str)."<br>";
    echo ucfirst(strtolower($str))."<br>";
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容