现在的位置: 首页 > 综合 > 正文

PHP 之 字符串处理小结

2014年02月03日 ⁄ 综合 ⁄ 共 1829字 ⁄ 字号 评论关闭
<?php

// 3456
echo substr ( "123456", 2, 4 );
echo "<br/>";

$str = "123";
// 不再使用echo $str[1];
echo $str {0};

// this hello today
echo "this
			hello
				today";

// thisisme
echo "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's
	a 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 test
echo 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/>";

/*
 * 	1326702908
	1128290400
	1326720908
	1327307708
	1327592113
	1327273200
	1326582000
 */
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/>";

//Php3
echo ucfirst($str);
echo "<br/>";

//This Is A Test
echo 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();
?>

抱歉!评论已关闭.