php入门5之字符串处理

一、strlen字符串长度:     php

    $a='1234abc';
    echo strlen($a);
函数

二、strtoupper与ucwords大小写转换: spa

     $a='i am A student.';
    echo strtoupper($a)."<br>";     // I AM A STUDENT.
    echo strtolower($a)."<br>";     // i am a student.
    echo ucfirst($a)."<br>";        // I am A student.
    echo ucwords($a)."<br>";        // I Am A Student. code

三、substr字符串截取: 字符串

       格式substr(string str,int start,int length); string

        $a='i am A student.';
        echo substr($a,0,1)." ";    // i
        echo substr($a,-1,1)." ";   // .
        echo substr($a,0)."  ";      // i am A student.
 
四、strstr与strrchr字符串查找: class

    格式  strstr(string haystack,string needle); 语法

strrchr()函数获取字符串(A)在另外一个字符串(B)中最后一次出现的位置,区分字母大小写。语法格式与strstr相同。
word

<?php
$a='i am A student.';
echo strstr($a,"am");  //am A student.
echo strrchr($a,"t");  //t.
?>


 五、str_ireplace字符串替换: co

    格式str_ireplace(string  search, string replace,string subject [,int &count])

     其中参数search为指定要查找的字符串;replace为指定替换的值;subject为指定查找的范围;count为可选参数,获取执行替换的数量。

    

<?php
$a='i am A student.';
$c=0;
echo str_ireplace("i am A","I am a",$a);  //I an a student.
echo str_ireplace("t","T",$a,$c);  //I am A sTudenT.
echo $c;                            //2
?>
相关文章
相关标签/搜索