php -- 字串操作

來源:互聯網
上載者:User

標籤:style   class   blog   c   code   java   

----- 014-string.php -----

 1 <!DOCTYPE html> 2 <html> 3 <head> 4     <meta http-equiv="content-type" content="text/html; charset=utf-8"> 5     <title>一個PHP網頁</title> 6 </head> 7 <body> 8 <pre> 9 <?php10     echo "<h3>字串比較</h3>";11     $str1 = "hello";12     $str2 = "HELLO";13     printf("%s比%s:%d\n", $str1, $str2, strcmp($str1, $str2));14     printf("%s比%s不區分大小寫:%d\n", $str1, $str2, strcasecmp($str1, $str2));15     $str1 = "helWWW";16     $str2 = "HELQQQ";17     printf("%s比%s,前三個字元:%d\n", $str1, $str2, strncmp($str1, $str2, 3));18     printf("%s比%s,前三個字元,不區分大小寫:%d\n", $str1, $str2, strncasecmp($str1, $str2, 3));19 ?>20 <?php21     echo "<h3>字串替換</h3>";22     $str = "葫蘆";23     $result = str_replace("葫蘆", "葫蘆娃", $str, $count);24     printf("葫蘆(葫蘆->葫蘆娃):%s。替換了%d次\n", $result, $count);25     $result = substr_replace($str, "葫蘆娃", 3, 3);//UTF-826     printf("葫蘆(2+2->葫蘆娃):%s。\n", $result);27 ?>28 <?php29     echo "<h3>截取字串</h3>";30     $str = "植物大戰大殭屍";31     printf("植物大戰大殭屍,從大開始:%s\n", strchr($str, "大"));32     printf("植物大戰大殭屍,從大開始,取左半部分:%s\n", strchr($str, "大", true));33     printf("植物大戰大殭屍,大的位置:%d\n", strpos($str, "大", 0));34     printf("植物大戰大殭屍,大的位置:從第三個字開始%d\n", strpos($str, "大", 9));35     printf("植物大戰大殭屍,從右邊的大開始:%s\n", strrchr($str, "大"));36     printf("植物大戰大殭屍,從第二個字元開始:%s\n", substr($str, 3));37     printf("植物大戰大殭屍,大出現次數:%s\n", substr_count($str, "大"));38 ?>39 <?php40     echo "<h3>分割字串</h3>";41     $str = "植物 大戰 殭屍";42     $s = strtok($str, " ");43     while ($s) {44         echo $s, "\n";45         $s = strtok(" ");46     }47     foreach(explode(" ", $str) as $temp){48         echo $temp, "\t";49     }50     echo implode("+", array("植物", "大戰", "殭屍"));51 ?>52 <?php53     echo "<h3>大小寫轉換</h3>";54     echo strtolower("PHP"), "\n";55     echo strtoupper("java"), "\n";56     echo ucfirst("what you see is what you get"), "\n";57     echo ucwords("what you see is what you get"), "\n";58     foreach(count_chars("abbcccdddd", 1) as $key=>$value){59         echo $key, "=>", $value, "\n";60     }61 ?>62 <?php63     echo "<h3>字串填充</h3>";64     $arr = array("柯南", "海賊王", "火影忍者", "櫻桃小丸子");65     foreach ($arr as $value) {66         echo str_pad($value, 18, "—", STR_PAD_LEFT);67         echo "\n";68     }69 ?>70 <?php71     echo "<h3>字串剔除</h3>";72     $str = "**  滴滴動力**  ";73     echo "待剔除字串:|", $str, "|\n";74     echo "剔除空格|", trim($str), "|\n";75     echo "剔除星號|", trim($str, "*"), "|\n";76     echo "剔除星號空格|", trim($str, "* "), "|\n";77     echo "左剔除星號空格|", ltrim($str, "* "), "|\n";78     echo "右剔除星號空格|", rtrim($str, " *"), "|\n";79 ?>80 </pre>81 </body>82 </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.