首頁  >  文章  >  後端開發  >  php常用函數-字串

php常用函數-字串

WBOY
WBOY原創
2016-07-29 08:50:411074瀏覽

學習php的過程中,整理的一些常用的函數,這是字串函數。

header("Content-Type:text/html;charset=UTF-8");
//刪除兩邊(單邊)空格或其它預定義字元
$str = "##hello world@@ ";
echo trim($str,'#,@')."
"; //hello world
echo ltrim($str,'#')."
"; //hello world @@
echo rtrim($str,'@')."
"; //##hello world
/* chop()是rtrim()的別名*/
//返迴路徑中的目錄部分
echo dirname("c:/testweb/home.php")."
"; //c:/testweb
/**字串生成與轉化*/
//把字串填入指定的字串中
$str = "hello world";
echo str_pad($str,20,'.',STR_PAD_BOTH)."
"; //....hello world.....
echo str_pad( $str,20,'.',STR_PAD_LEFT)."
"; //.........hello world
echo str_pad($str,20,'.',STR_PAD_RIGHT).""; //hello world.........
//重複使用指定的字串
echo str_repeat(".",13)."
"; //... ..........
/* 13為重複的次數*/
//把字串分割到數組中(包含空格,一個空格為1個字元)
$str = "my name is Junjun Liu";
$str1 = str_split($str,3);
print_r($str1); //Array ( [0] => my [1] => nam [2] => e i [3] => s J [4] => unj [5] => un [6] => Liu )
//反轉字串
$str = "imagecreatetruecolor";
echo strrev($str)."
"; //roloceurtetaercegami
//依照指定長度對字串進行拆行處理
$str = "An example on a long word is: Supercalifragulisticlasdkjflasdjfalsdkakd";
echo wordwrap($str,20, "
");
//隨機打亂字串中所有的字元(數字被打亂時間隔符號也會隨著打亂)
$str = "a1,b2,c3";
echo str_shuffle($str)."
"; //1,ca32,b(隨機的一種)
//將字串解析成變數
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first."
"; // value
echo $ arr[0]."
"; // foo bar
echo $arr[1]."
"; // baz
print_r($arr); //Array ( [0] => foo bar [1] => baz )
parse_str("id=23&name=John%20Adams",$myArray);
print_r($myArray); //Array ( [id] => 23 [name] => John Adams )
//透過千分位組格式化數字
echo number_format("1000000"); //1,000,000
echo number_format("1000000",2); //1,000,000.000,000. ,","."); //1.000.000,00
//大小寫轉換
echo strtolower('NAME'); //name
echo strtoupper('name'); //NAME
echo ucfirst(' my name is Junjun Liu'); //My name is Junjun Liu
echo ucwords('my name is Junjun Liu'); //My Name Is Junjun Liu
//html關聯標籤
//把字串轉為HTML實體
$str = "John & 'Adams'";
echo htmlentities($str, ENT_COMPAT); //John & 'Adams'
//預定義字元轉html編碼
htmlspecialchars($str);
echo "echo "";
//把字串中的n r和回車換成
標籤,實現換行輸出
$string = "Thisrnisnranstringr";
echo nl2br($string);
/**
* 這個
* 是
* 一個
* 字串
*/
//剝去HTML,XML,PHP的標籤
$str="asdasd

woaini

";
echo strip_tags($str); //asdasdwoaini
echo "
";
//在指定的字元前面加上反斜線轉義字串中字元
$str = "Hello, My name is John Adams.";
echo addcslashes($str,'m '); //Hello, my name is John Adams
//刪除addcslashes反斜線
echo stripcslashes($str); //Hello, my name is John Adams.
echo "
";
//指定預定義字元前加上反斜線
$str = "Who's John Adams?";
$str = addslashes($str);
echo $str; //Who's John Adams?
//刪除反斜線
echo stripslashes($str)."
"; //Who's John Adams?
//在字串中某些預先定義的字元前面加上反斜線(所有的字元被轉義)
$str = " hello world.(can you hear me?)";
echo quotemeta($str)."
"; //hello world.(can you hear me?)
//ASCII回傳字元
echo chr(34 ); //傳回"
//傳回字串中第一個字元的ASCII碼值
echo ord(abc); //97
echo "
";
/**字串比較*/
/*
* 1:前者大
* -1:後者大
* 0:前後相等
*/
echo "不區分大小寫比較兩個字串:".strcasecmp("abc","abd"). "
"; //-1
echo "區別大小寫比較兩個字串:" .strcmp("abd","Abd")."
"; //1
echo "區別大小寫比較兩個字串:" .strncmp("abcd ","abcc",2)."
"; //0 /* 2是比較前n個字串大小*/
echo "不區分大小寫比較兩個字串:".strncasecmp("abcd ","abcc",4)."
"; //1
echo "區分大小(依自然順序)寫比較兩個字串:".strnatcmp("abc2","abc12").""; //-1
echo "不區分大小(以自然順序)寫比較兩個字串:".strnatcasecmp("Abc8","abc12")."
"; //-1
/* 字串切割與拼接*/
//將字串分成小塊(空格也算)
$str="hello world hello world";
echo chunk_split($str,2,"#"); / /he#ll#o #wo#rl#d #he#ll#o #wo#rl#d#
//切開字串
$first_token = strtok('/something', '/');
$second_token = strtok('/');
var_dump($first_token); //string(9) "something"
var_dump($second_token); //bool(false)
//var_dump($first_token,$second_token);同時印出兩個變數)
$str = "This is an /example string";
$tok = strtok($str,"/"); //This is an
echo $tok;
$str = "Thisisan / example string";
$tok = strtok($str,"/"); //Thisisan
echo $tok;
//使用字串為標誌分割另一個字串
$data = "foo:*:1023 :1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
//將陣列值用預訂字元連接成字串
$array = array('lastname', 'email', 'phone');
$ a = implode(",", $array);
echo $a; // lastname,email,phone
//截取字串
$str = "absadf";
echo substr($str,2,3); //sad
echo substr($str,-4,-1);//sad
/* 字串尋找與取代*/
//字串取代操作,區分大小寫str_replace(被換字體,換成的字符串,原字符串)
$str = "1,2,3:4,5:6";
echo str_replace(",",":",$str)."
"; // 1:2:3:4:5:6
echo str_replace(array(",",":"),";",$str)."
"; //1;2;3;4 ;5;6
echo str_replace(array(",",":"),array(";","#"),$str)."
"; //1;2;3#4 ;5#6
//字串替換操作,不區分大小寫
$str = "abcdefg";
echo str_ireplace("ABC","xyz",$str); //xyzdefg
//統計一個字串在另一個字串中出現的次數substr_count(在此字串中搜索,被搜尋的字串,開始偏移的位置,指定偏移的最大位置)
$str1 = "name";
$str2 = " my name isname name";
echo substr_count($str2,$str1); //2
//替換字串中某段為另一個字串
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original : $var
n"; //Original: ABCDEFGH:/MNRPQR/
/* 這兩個例子使用"bob" 來取代整個$var。 */
echo substr_replace($var, 'bob', 0) . "
n"; //bob
echo substr_replace($var, 'bob', 0, strlen($var)) . "
n"; //bob
/* 將"bob" 插入$var 的開頭。 */
echo substr_replace($var, 'bob', 0, 0) . "
n"; //bobABCDEFGH:/MNRPQR/
/* 下面兩個範例使用"bob" 取代$var 中的" MNRPQR"。 */
echo substr_replace($var, 'bob', 10, -1) . "
n"; //ABCDEFGH:/bob/
echo substr_replace($var, 'bob', -7, -1 ) . "
n"; //ABCDEFGH:/bob/
/* 從$var 移除"MNRPQR"。*/
echo substr_replace($var, '', 10, -1) . "
n"; //ABCDEFGH://
//傳回兩個字串相似度
$str1 = "abcdefgadfsa";
$str2 = "acdrgwsaasdf";
echo ((similar_text($str1,$str2)/strlen($str1))*100)."%"."
"; //58.333333333333%
//字串找出
$str = "zhangsan";
echo strstr($str,"a")."
"; //angsan 從前面開始找a出現的位置並截取到最後(預設false)別名:strchr ()
echo strstr($str,"a",true)."
"; //zh 從前面開始找a出現的位置並向前截取
echo strrchr($str,"a"). "
"; //an 從後面開始找a並截取到最後
echo strpos($str,"a")."
"; //2 取得字串首次出現a的位置
echo strpos($str,"a",3)."
"; //6 從3位置開始取得字串出現a的位置
echo strrpos($str,"a")."
"; //6 取得字串中a最後一次出現的位置
//轉換指定字元
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr ("hi all, I said hello", $trans); //hello all, I said hi
echo strtr("baab", "ab", "01"); //1001
$trans = array("ab " => "01");
echo strtr("baab", $trans); //ba01
/*
* strstr():區分大小寫
* stristr():不區分大小寫
*/
/ *
* strpos():區分大小寫
* stripos()不區分大小寫
* strrpos():區分大小寫
* strripos():不區分大小寫
*
*/
//返回計算字串中全部字元都存在於指定字元集合中的​​第一段子字串的長度。
$var = strspn("42 is the answer to the 128th question.", "1234567890");
echo $var; //2 因為'42' 是subject 中第一段的全部字符都存在於'1234567890'連續字元。
//取得不符合遮罩的起始子字串的長度
$a = strcspn('abcd', 'apple'); var_dump($a); //int(0)
$b = strcspn(' abcd', 'banana'); var_dump($a); //int(0)
$c = strcspn('hello', 'l'); var_dump($c); //int(2)
$d = strcspn('hello', 'world'); var_dump($d); //int(2)
/* 字串統計*/
//統計字串含有的單字數(第三個參數??? ?
$str = "My name is John";
echo str_word_count($str); //4
print_r(str_word_count($str,1)); //Array ( [0] => My [1] => name [2] => is [3] => John )
print_r(str_word_count($str,2)); //Array ( [0] => My [3] => name [8] => is [11] => John )
//統計字串長度
$str = ' ab cd ';
echo strlen($str); // 7
//統計字串所有字母出現次數(0,255),每個字元出現的次數,以對應的ascii碼值表示其出現的數量
$str = "aaaaasdfasdfwer;dlfgjjpoertuodbldbnlskjl;asfjoiwertowitwo";
echo "
";<br>//print_r(count_chars(echo "<pre class="brush:php;toolbar:false">";<br>//print_r(count_chars(); pre>";<br>//md5<br>$str = "hello4521";<br>echo md5($str); //5af267d811a324fd640b7ad2199dfe14<br>echo "<hr>"; (md5($s)."tri");<br>}<br>*/<br>//md5_file()<br>$str = "ly.db";<br>echo md5_file($str); //2f2b2262ed0732d497c90bf
                
                
                <br>
                    以上就介紹了php常用函數-字串,包含了php,字串方面的內容,希望對PHP教學有興趣的朋友有幫助。 <br>
                <br>
                    <br>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn