PHP字符处理函数(一)「原」

The PHP handler characters (一)

Posted by LingDie | 靈蝶 on December 27, 2014

下滑这里查看更多内容

1.strpos查找字符串首次出现的位置

mixed strpos(string $haystack, mixed $needle, [, int $offset=0]) 返回needle在haystack中首次出现的数字位置。 参数: haystack:在该字符串中进行查找 needle:如果needle不是一个字符串,那么它将被转换为整型并被视为字符的顺序值 offset:如果提供了此函数,搜索会从字符串该字符数的起始位置开始统计。

    <?php  
    $mystring = 'abc';  
    $findme = 'a';  
    $pos = strpos($mystring, $findme);  
      
    if ($pos === false) {  
        echo "The string '$findme' was not found in the string '$mystring'";  
    }  
    else {  
        echo "The string '$findme' was found in the string '$mystring'";  
        echo " and exists at position $pos";  
    }  
    ?>  

输出为 The string ‘a’ was found in the string ‘abc’ and exists at position 0

2.substr返回字符串的字串

string substr(string $string, int $start [, int $length]) 返回字符串string由start和length参数指定的子字符串。 参数: string:输入字符串 start:表示从什么位置返回字符串,可以为负数 length:返回的字符串将从start处开始最多包括length个字符,可以为负数

    <?php  
    $myname = 'WANGPeng';  
    $familyname = substr($myname,0,4);  
    print $familyname;  
    ?> 

输出为WANG

3.substr_replace 替换字符串的子串

mixed substr_replace (mixed $string, mixed $replacement, mixed $start [, mixed $length ] ) substr_replace()在字符串string的副本中将由start和可选的length参数限定的子字符串使用replacement进行替换。 参数: string:输入字符串 replacement:替换字符串 start:如果start为正数,替换将从string的start位置开始。也可以为负数。 length:如果为整数,表示string中被替换的子字符串的长度。

     <?php  
    $var = 'ABCDEFGH:/MNRPQR/';  
    echo "Original: $var<br/>";  
    echo substr_replace($var, 'bob', 0)."<br/>";  
    echo substr_replace($var, 'bob', 0, 5);  
    ?>  

输出为:

Original: ABCDEFGH:/MNRPQR/ bob bobFGH:/MNRPQR/

4.逐字节处理字符串

利用for循环遍历字符串的每一个字节。

     <?php  
    $string = "This weekend, I'm going shopping for a pet chicken.";  
    $vowels = 0;  
    for ($i=0, $j=strlen($string); $i < $j; $i++){  
        if(strstr('aeiouAEIOU',$string[$i])){  
            $vowels++;  
        }  
    }  
    echo $vowels;  
    ?>  

输出14

5.按字(array_reverse)或按字节(strrev函数)来反转字符串

string strrev(string $string) 返回string反转后的字符串

    <?php  
    print strrev('This is not a palindrome.');  
    ?>  

输出.emordnilap a ton si sihT

array array_reverse(array $array [, bool $preserve_keys = false]) array_reverse()接受数组array作为输入并返回一个单元为相反顺序的新数组。 参数: array:输入的数组 preserve_keys:如果设置为 TRUE 会保留数字的键。 非数字的键则不受这个设置的影响,总是会被保留。

    <?php  
    $s = "Once upon a time there was a turtle.";  
    #将字符串分解为独立的字  
    $words = explode(' ',$s);  
    #反转这个字符组  
    $words = array_reverse($words);  
    #重组反转后的字符串  
    $s = implode(' ',$words);  
    print $s;  
    ?>  

turtle. a was there time a upon Once

6.str_replace

mixed str_replace(mixed $search, mixed $replace, mixed $subject [, int &$count ]) 该函数返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果。 参数: search:查找的目标值,也就是needle。一个数组可以指定多个目标。 replace:search的替换值。一个数组可以被用来指定多重替换。 subject:执行替换的数组或者字符串。也就是haystack。 如果是数组,替换操作将遍历整个subject,返回值也将是一个数组。 count:如果被指定,它的值将被设置为替换发生的次数。

    <?php  
    #赋值:<body text='black'>  
    $bodytag = str_replace("black", "red", "Paper is balck");  
    echo $bodytag;  
    echo "<br>";  
      
    #赋值:Hll wrld f PHP  
    $vowels = array("a","e","i","o","u","A","E","I","O","U");  
    $onlyconsonants = str_replace($vowels,"","Hello world of PHP");  
    echo $onlyconsonants;  
    echo "<br>";  
      
    #赋值 You should eat pizza, beer, and ice cream every day  
    $phrase = "You should eat fruits, vegetables, and fiber every day.";  
    $healthy = array("fruits", "vegetables", "fiber");  
    $yummy = array("pizza", "beer", "ice cream");  
      
    $newphrase = str_replace($healthy, $yummy, $phrase);  
    echo $newphrase;  
    echo "<br>";  
      
    #赋值:2  
    $str = str_replace("ll","","good golly miss molly",$count);  
    echo $count;  
    ?> 

 

输出为:

Paper is red Hll wrld f PHP You should eat pizza, beer, and ice cream every day. 2