PHP常用字符串操作函數(shù)實(shí)例總結(jié)(trim、nl2br、addcslashes、uudecode、md5等)
本文實(shí)例總結(jié)了PHP常用字符串操作函數(shù)。分享給大家供大家參考,具體如下:
/*常用的字符串輸出函數(shù) * * echo() 輸出字符串 * print() 輸出一個(gè)或多個(gè)字符串 * die() 輸出一條信息,并退出當(dāng)前腳本 * printf() 輸出格式化字符串 * sprintf() 把格式化的字符串寫入到一個(gè)變量中 * */ //ucfirst //將字符串中的首字母轉(zhuǎn)換為大寫 $str="string"; echo ucfirst($str); echo "<hr><br/>"; //ucwords() //將字符串中的每個(gè)單詞的首字母大寫 $ucword="hello everyone!"; echo ucwords($ucword); echo "<hr><br/>"; //ltrim() rtrim() trim() //去除空格 $str="123 This is a test....."; echo ltrim($str,"0..9")."<br/>"; //去除左側(cè)的數(shù)字 echo rtrim($str,".")."<br/>"; echo trim($str,"0..9A..Z.")."<br/>"; //去除字符串兩端的大寫字母,數(shù)字還有. //HTML相關(guān)的字符串格式化函數(shù) //nl2br() //將字符串中的\n轉(zhuǎn)換為"<br/>" $str="this is \n hello world"; echo nl2br($str).'<br/>'; //htmlspecialchars() //將html標(biāo)記以字符的形式顯示,不進(jìn)行解釋 $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的方法對(duì)字符串進(jìn)行編碼 $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方法相反,將進(jìn)行編碼后的html字符轉(zhuǎn)換為瀏覽器能夠編譯的形式 $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函數(shù)相反,將HTML實(shí)體轉(zhuǎn)換為字符 $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 ] ) //對(duì)文件進(jìn)行md5加密 // $string="password"; $str=md5($string); if($str=="5f4dcc3b5aa765d61d8327deb882cf99"){ echo "The password is right <br/>"; } //parse_str ( string $str [, array &$arr ] ) //將一個(gè)字符串進(jìn)行解析,解析成變量和數(shù)組的形式 $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 ] ) //計(jì)算文件的散列值 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 ] ) //計(jì)算兩個(gè)字符串的相似度,通過(guò)引用方式傳遞第三個(gè)參數(shù),similar_text() 將 //計(jì)算相似程度百分?jǐn)?shù)。 $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 ) //打亂一個(gè)字符串 $string="I want you to solve this problem"; echo str_shuffle($string)."<br/>"; //array str_split ( string $string [, int $split_length = 1 ] ) //按照指定的長(zhǎng)度對(duì)字符串進(jìn)行分割 $arr=str_split($string,3); //str_word_count ( string $string [, int $format = 0 [, string $charlist ]] ) //統(tǒng)計(jì)字符串中單詞的數(shù)量 echo "<hr><br/>"; //int strripos ( string $haystack , string $needle [, int $offset = 0 ] ) //以不區(qū)分大小寫的方式查找指定字符串在目標(biāo)字符串中最后一次出現(xiàn)的位 //置。與 strrpos() 不同,strripos() 不區(qū)分大小寫。 //offset用于指定從那個(gè)位置開始查找 $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 第一次出現(xiàn)的位置開始到 haystack 結(jié) //尾的字符串。 該函數(shù)區(qū)分大小寫。如果想要不區(qū)分大小寫,請(qǐng)使用 //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中出現(xiàn)的次數(shù),$needle區(qū)分大小寫 $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 ]]] ) //正則匹配,將匹配后的結(jié)果存放到$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進(jìn)行替換. $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 ]] ) //通過(guò)一個(gè)正則表達(dá)式分隔給定字符串. $str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars);
更多關(guān)于PHP字符串操作相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php字符串(string)用法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php圖片的二進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法
這篇文章主要介紹了php圖片的二進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法,詳細(xì)介紹了圖片與二進(jìn)制之間轉(zhuǎn)換的原理與實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12thinkphp5實(shí)用入門進(jìn)階知識(shí)點(diǎn)和各種常用功能代碼匯總
這篇文章主要介紹了thinkphp5實(shí)用入門進(jìn)階知識(shí)點(diǎn)和各種常用功能代碼匯總的相關(guān)資料,需要的朋友可以參考下2023-03-03escape unescape的php下的實(shí)現(xiàn)方法
escape unescape的php下的實(shí)現(xiàn)方法...2007-04-04關(guān)于php支持分塊與斷點(diǎn)續(xù)傳文件下載功能代碼
一篇關(guān)于php流下載,就是可以支持分塊與斷點(diǎn)續(xù)傳文件下載,有需要的朋友可以看看2014-05-05同一空間綁定多個(gè)域名而實(shí)現(xiàn)訪問(wèn)不同頁(yè)面的PHP代碼
同一空間綁定多個(gè)域名而實(shí)現(xiàn)訪問(wèn)不同頁(yè)面的PHP代碼...2006-12-12PHP實(shí)現(xiàn)二維數(shù)組按照指定的字段進(jìn)行排序算法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)二維數(shù)組按照指定的字段進(jìn)行排序算法,涉及php針對(duì)數(shù)組的遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04