PHP 之 字符串处理小结

来源:互联网 发布:越南语发音软件 编辑:程序博客网 时间:2024/05/21 17:44
<?php// 3456echo substr ( "123456", 2, 4 );echo "<br/>";$str = "123";// 不再使用echo $str[1];echo $str {0};// this hello todayecho "thishellotoday";// thisismeecho "this", "is", "me";echo "<br/>";$str = "php";$number = 232;printf ( "%0.3f<br/>", $number );echo "<br/>";$format = "the %2\$s book contains %1\$d pages. that'sa nice %2\$s full of %1\$d pages. <br/>";printf ( $format, $number, $str );// 12// 6$str = "      php2  ";echo strlen ( $str );echo "<br/>";echo strlen ( ltrim ( $str ) );echo "<br/>";// this is a test..$str = "123 This is a test...";echo ltrim ( $str, "0..9" );echo "<br/>";echo rtrim ( $str, "." );// his is a testecho trim ( $str, "0..9 A..Z." );echo "<br/>";/* * php3 ---php3--- ------php3 */$str = "php3";echo str_pad ( $str, 10 ); // 默认在右边echo "<br/>";echo str_pad ( $str, 10, "-", STR_PAD_BOTH );echo "<br/>";echo str_pad ( $str, 10, "-", STR_PAD_LEFT );echo "<br/>";echo strtoupper ( $str );echo "<br/>";/* * 1326702908112829040013267209081327307708132759211313272732001326582000 */echo (strtotime ( "now" ));echo "<br/>";echo (strtotime ( "3 October 2005" ));echo "<br/>";echo (strtotime ( "+5 hours" ));echo "<br/>";echo (strtotime ( "+1 week" ));echo "<br/>";echo (strtotime ( "+1 week 3 days 7 hours 5 seconds" ));echo "<br/>";echo (strtotime ( "next Monday" ));echo "<br/>";echo (strtotime ( "last Sunday" ));echo "<br/>";//Php3echo ucfirst($str);echo "<br/>";//This Is A Testecho ucwords("this is a test");//echo ucfirst(strtolower($str));echo "<br/>";/* * One line.next line */echo nl2br("One line.\n next line");echo "<br/>";$str = "<B>WebServer:</B>&'Linux'&'Apache'";//WebServer:&'Linux'&'Apache',黑体echo $str."<br/>";//不会有标签效果//<B>WebServer:</B>&'Linux'&'Apache'//源码:<B>WebServer:</B>&'Linux'&'Apache'echo htmlspecialchars($str,ENT_COMPAT,"UTF-8");//转换HMTL和双引号echo "<br/>";$str = "一个 'quote' 是 <b>bold</b>";//Ò»¸ö 'quote' ÊÇ <b>bold</b>//htmlentities() 可以将所有非ASCII码字符转化为 对应的实体代码//可以转义更多的HTML字符echo htmlentities($str);echo "<br/>";//$str = "一个 'quote' 是 <b>bold</b>";//没有出中文echo htmlentities($str,ENT_QUOTES,'utf-8');echo phpinfo();?>