LAMP之路

不积跬步,无以至千里!

[PHP函数]PHP字符串函数

2012-10-18 一抹阳光 PHP

<pre>
<?php
/*PHP字符串函数
*2009.12.25
*/
echo ‘<b>1.strlen()、strstr()、strpos()/strrpos()、str_replace()、 str_repeat()、substr()、substr_count()、substr_replace()</b><br />’;
//strstr() 如果没有找到 则传回 false。
$email = ‘sterling@designmultimedia.com’;
$domain = strstr ($email, ‘@’);
print $domain;     // prints @designmultimedia.com
echo ‘<br />——-<br />’;
//strpos()、strrpos() 如果找不到参数 needle,则传回 false
$pos = strpos (‘mybabystring’, “b”);
if ($pos === false) {    // note: three equal signs
// not found…
} else    echo $pos;
echo ‘<br />——-<br />’;
//str_replace()
print htmlspecialchars(str_replace (“%body%”, “black”, “<body text=%body%>”));
echo ‘<br />——-<br />’;
//str_replace()第二个参数必须大于 0
echo str_repeat (“-=”, 10);
echo ‘<br />——-<br />’;
//substr() 如果 start是负数,传回的字符串将会从 string结尾的第 start个字开始。如果有给予参数 length而且是负数时,传回的字符串将会结束于 string结尾的第 length个字元。
print substr (“abcdef”, -1);       // returns “f”
print substr (“abcdef”, -2);       // returns “ef”
print substr (“abcdef”, -3, 1);   // returns “d”
print substr (“abcdef”, 1, -1);   // returns “bcde”
echo ‘<br />——-<br />’;
//substr_count()计算字符串的出现次数
print substr_count(“This is a test”, “is”);    // prints out 2
echo ‘<br />——-<br />’;
//substr_replace() 用指定的字符串替换截取出来的字符串,规则同substr()。
$var = ‘ABCDEFGH:/MNRPQR/’;
echo substr_replace ($var, ‘bob’, 10, -1) . “<br>\n”;
echo substr_replace ($var, ‘bob’, -7, -1) . “<br>\n”;
echo ‘<br />—————————————————<br />’;

echo ‘<b>2.explode()/implode()、trim()/ltrim()/rtrim()</b><br />’;
$pizza = “piece1 piece2 piece3 piece4 piece5 piece6″;
$pieces = explode (” “, $pizza);
var_dump($pieces);
$pizza = implode (“,”, $pieces);
var_dump($pizza);
echo ‘<br />—————————————————<br />’;

echo ‘<b>3.htmlspecialchars()/htmlspecialchars_decode()</b><br />’;
//htmlspecialchars()/htmlspecialchars_decode() 特殊字元和HTML实体的相互转换
echo ‘<br />—————————————————<br />’;

echo ‘<b>4.strtoupper()/strtolower()、ucfirst()、ucwords()</b><br />’;
//strtoupper()/strtolower() 字符串大小写相互转换
$str = “Mary Had A Little Lamb and She LOVED It So\n”;
$str = strtoupper($str);
print $str;
$str = strtolower($str);
print $str;
echo ‘<br />——-<br />’;
//ucfirst() 字符串首字母的大写
$text = ‘mary had a little lamb and she loved it so.’;
$t1 = ucfirst ($text);
print $t1;
echo ‘<br />——-<br />’;
//ucwords() 字符串中各个单词的首字母的大写
$t1 = ucwords($text);
print $t1;
echo ‘<br />—————————————————<br />’;

echo ‘<b>5.strcmp()/strcasecmp()、strncmp()/strncasecmp()</b><br />’;
//strcmp() str1小于 str2则传回小于零的值;如果 str1大于 str2则传回大于零的值;若二字符串相等则传回零。区分大小写。
//strcasecmp  不分大小写。
//strncmp(string str1,string str2,int length)    比较两字符串的前length位的大小
echo ‘<br />—————————————————<br />’;

echo ‘<b>6.sprintf()/printf()/print、sscanf()</b><br />’;
//类似C语言的输入输出语句
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
printf (“%01.2f”, $money);
$formatted = sprintf (“%01.2f”, $money);
print $formatted;
echo ‘<br />——-<br />’;
$serial = sscanf(“SN/2350001″,”SN/%d”);
var_dump($serial);
$mandate = “January 01 2000″;
list($month, $day, $year) = sscanf($mandate,”%s %d %d”);
echo “Item $serial was manufactured on: $year-”.substr($month,0,3).”-$day\n”;
echo ‘<br />—————————————————<br />’;
?>