PHP常用字串操作函數執行個體總結(trim、nl2br、addcslashes、uudecode、md5等)

來源:互聯網
上載者:User

標籤:

 

/*常用的字串輸出函數** echo() 輸出字串* print() 輸出一個或多個字串* die() 輸出一條資訊,並退出當前指令碼* printf() 輸出格式化字串* sprintf() 把格式化的字串寫入到一個變數中**///ucfirst //將字串中的首字母轉換為大寫$str="string";echo ucfirst($str);echo "<hr><br/>";//ucwords()//將字串中的每個單詞的首字母大寫$ucword="hello everyone!";echo ucwords($ucword);echo "<hr><br/>";//ltrim() rtrim() trim()//去除空格$str="123 This is a test.....";echo ltrim($str,"0..9")."<br/>"; //去除左側的數字 echo rtrim($str,".")."<br/>";echo trim($str,"0..9A..Z.")."<br/>"; //去除字串兩端的大寫字母,數字還有.//HTML相關的字串格式化函數//nl2br()//將字串中的\n轉換為"<br/>"$str="this is \n hello world";echo nl2br($str).‘<br/>‘;//htmlspecialchars()//將html標記以字元的形式顯示,不進行解釋$str="<b>hello world</b>";echo $str."<br/>";echo htmlspecialchars($str);echo "<hr><br/>";//addcslashes//添加反斜線$str=addcslashes("foo[]","A..z");echo $str."<br/>";echo addcslashes("zoo[‘.‘]",‘A..z‘)."<br/>";//convert_uuencode()//利用uudecode的方法對字串進行編碼$string="hello world";$str= convert_uuencode($string);echo $str."<br/>";echo convert_uudecode($str)."<br/>";//html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ‘UTF-8‘ ]] )//與htmlentities方法相反,將進行編碼後的html字元轉換為瀏覽器能夠編譯的形式$a="I want a bright <b>future</b>";$b= htmlentities($a)."<br/>";echo $b;echo html_entity_decode($b);echo "<hr><br/>";//htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )//與htmlspecialchars函數相反,將HTML實體轉換為字元$c=htmlspecialchars($a);echo $c."<br/>";echo htmlspecialchars_decode($c)."<br/>";echo "<hr><br/>";//lcfirst ( string $str )//將字串的首字元小寫$str="Hello World";// echo lcfirst($str)."<br/>";//md5_file ( string $filename [, bool $raw_output = false ] )//對檔案進行md5加密//$string="password";$str=md5($string);if($str=="5f4dcc3b5aa765d61d8327deb882cf99"){ echo "The password is right <br/>";}//parse_str ( string $str [, array &$arr ] )//將一個字串進行解析,解析成變數和數組的形式$str = "first=value&arr[]=foo+bar&arr[]=baz";parse_str($str,$input);print_r($input);echo "<hr><br/>";//string sha1_file ( string $filename [, bool $raw_output = false ] )//計算檔案的散列值foreach(glob("C:/lamp/appache2/htdocs/*.php") as $ent){ if(is_dir($ent)){ continue; } echo $ent."(SHA1:".sha1_file($ent).")<br/>";}echo "<hr><br/>";//int similar_text ( string $first , string $second [, float &$percent ] )//計算兩個字串的相似性,通過引用方式傳遞第三個參數,similar_text() 將//計算相似程度百分數。$string1="rogerzhalili";$string2="zhangjieroger";if(similar_text($string1,$string2,$percent)){ echo $string1." and ".$string2." has the similarity of:".$percent."<br/>";}echo "<hr><br/>";//string str_shuffle ( string $str )//打亂一個字串$string="I want you to solve this problem";echo str_shuffle($string)."<br/>";//array str_split ( string $string [, int $split_length = 1 ] )//按照指定的長度對字串進行分割$arr=str_split($string,3);//str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )//統計字串中單詞的數量echo "<hr><br/>";//int strripos ( string $haystack , string $needle [, int $offset = 0 ] )//以不區分大小寫方式尋找指定字串在目標字串中最後一次出現的位置。與 strrpos() 不同,strripos() 不區分大小寫。//offset用於指定從那個位置開始尋找$haystack=‘ababcd‘;$needle=‘Ab‘;echo "the last".$needle."postion is:".strripos($haystack,$needle)."<br/>";echo strrpos($haystack,‘ab‘);echo "<hr><br/>";//string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )//返回 haystack 字串從 needle 第一次出現的位置開始到 haystack 結尾的字串。 該函數區分大小寫。如果想要不區分大小寫,請使用//stristr()。$a="the First test";$needle="Fi";echo strstr($a,$needle)."<br/>";if($c=strstr($a,"Fio")){ echo "find".$c."<br/>";}else{ echo "not find the string!<br/>";}echo "<hr><br/>";//int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )//尋找$needle子字串在$haystack中出現的次數,$needle區分大小寫$hay="la la wa la wa wa lala";echo substr_count($hay,"la")."<br>";//int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )//正則匹配,將匹配後的結果存放到$matches(如果指定了$matches的話)preg_match_all("/?(\d3)?? (?(1) [\-\s] ) \d{3}-\d{4}/x","Call 555-1212 or 1-800-555-1212", $phones);echo "<pre>";print_r($phones);echo "</pre>";echo "<hr><br/>";//preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )//搜尋subject中匹配pattern的部分, 以replacement進行替換.$string = ‘April 15, 2003‘;$pattern = ‘/(\w+) (\d+), (\d+)/i‘;$replacement = ‘${1}1,$3‘;echo preg_replace($pattern,$replacement,$string);echo "<hr><br/>";//array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )//通過一個Regex分隔給定字串.$str = ‘string‘;$chars = preg_split(‘//‘, $str, -1, PREG_SPLIT_NO_EMPTY);print_r($chars);

 

 

相關連結

 

PHP常用字串操作函數執行個體總結(trim、nl2br、addcslashes、uudecode、md5等)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.