php字串函式的一些理解

2021-06-29 04:35:25 字數 1313 閱讀 8690

<?php

$word="my name is janvinal";

echo str_word_count($word)."

";$str1="serwtredasdfadf asasasasas";

$str2="as";

$ans=strstr($str1, $str2);

echo "strstr: ".$ans."

";$ans=stristr($str1, $str2);

echo "stristr: ".$ans."

";$ans=strpos($str1, $str2);

echo "strpos: ".$ans."

";$ans=stripos($str1, $str2);

echo "stripos: ".$ans."

";$ans=strcmp($str1, $str2);

echo "strcmp: ".$ans."

";$ans=strncmp($str1, $str2, 1);

echo "strncmp: ".$ans."

";$ans=strnatcmp($str1, $str2);

echo "strnatcmp: ".$ans."

";$str1="a p";

$str2=array("a", "p");

$count=0;

//echo $str1."

"."$str2"."

"."$str3"."

";$ans=str_replace($str2, $str3, $str1, $count);

echo "str_replace: ".$ans."

";echo $count."

";$ans=str_ireplace($str2, $str3, $str1, $count);

echo "str_ireplace: ".$ans."

";echo $count."

";/*

結果是:

str_replace: ale

4str_ireplace: ale

4分析:

首先字串str1被字元陣列str2匹配,然後替換成字元陣列str3,這裡

替換原則需要注意,str2是乙個陣列,那麼它在和str3替換的時候是按照

因為str3沒有下標為2的字串,所以str2的"p"匹配空串;

那麼現在替換開始進行替換str1中的"a p"和str2中的字串都匹配到了,所以

所以最後結果是"ale",這裡php不計算空格

*/?>

常用的一些PHP字串函式

基本的常用的字串函式 strlen str 返回字串長度 mb strlen str 可以返回中文字元長度 strtolower str 字母轉小寫 strtoupper str 字母轉大寫 ucwords str 每個單詞的首字母轉大寫 ucfirst str 首字母轉大寫 str replace...

一些字串函式

1.right location,somenumber left location,somenumber select right location,2 from my contacts 返回location列中所有右數兩個字元 select left location,2 from my cont...

字串的一些函式

單字元超作 獲取字串長度strlen strsrc char str3 hello int len len strlen str3 printf strlen d len 它純在這一點侷限 但字元陣列中有 0 時,字元長度返回值是錯誤的。倆個字元超作 複製字串超作函式 strcpy strdet s...