PHP常用函数系列之字符串处理
除字符串右端的空白字符或其他预定义字符
chop(string,charlist)
函数示例:
function funcChop() {
$str = "Hello YLD!";
echo $str . "<br>";
echo chop($str,"YLD!"). "<br>";
echo rtrim($str,"YLD!")."<br>";//chop()是此函数的别名,可以理解为小名。
}
Hello YLD!
Hello
Hello
function funcLtrim() {
$str = "~Hello small yellow luo";
echo $str."<br>";
echo ltrim($str,"~Hello")."<br>";
echo ltrim($str,"~he")."<br>";//区分大小写的;字符串必须连贯
echo ltrim($str,"ll")."<br>";//必须从左侧第一个字符开始;
}
~Hello small yellow luo
small yellow luo
Hello small yellow luo
~Hello small yellow luo
function funcTrim() {
$question = " \0 what's up? \r";
$answer = " no";
var_dump($question.$answer);
var_dump(trim($question).trim($answer));
}
string(18) " what's up? no"
string(12) "what's up?no" //注意字符串的个数变化
<br />
或 <br>
,并返回。function funcNl2br() {
echo nl2br("luo is \n ugly \r\n");
$string = "Small\r\nYellow\n\rLuo\nis\rstupid";
echo nl2br($string,false);//注意输出换行
}
luo is
ugly
Small
Yellow
Luo
is
stupid
printf ( string $format [, mixed $args [, mixed $... ]] )
function funcPrintf() {
//与java中格式化输出一样
printf('I need to pay $%.02lf',1.3568);
echo "<br>";
$goodevil = array('good', 'evil');
//巧用printf
printf_array('There is a difference between %s and %s', $goodevil);
}
function printf_array($format, $arr)
{
//回调printf函数
return call_user_func_array('printf', array_merge((array)$format, $arr));
}
I need to pay $1.36
There is a difference between good and evil
function funcSprintf() {
$number = 2;
$location = "HangZhou";
//与printf相比,只有格式化的功能,没有打印的功能
$text = sprintf("I have %u friends in %s",$number,$location);
echo $text;
}
I have 2 friends in HangZhou
1.2
输出1.20
,可采用此函数。function funcStrtoupper() {
$str = "i want to become upper";
echo $str."<br>";
$str = strtoupper($str);
echo $str;
}
i want to become upper
I WANT TO BECOME UPPER
function funcStrtolower() {
$str = "I WANT TO BECOME LOWER";
echo $str."<br>";
$str = strtolower($str);
echo $str;
}
I WANT TO BECOME LOWER
i want to become lower
function funcUcfirst() {
//upper capitalize first的缩写,maybe
$str = "i want to become upper";
echo $str."<br>";
$str = ucfirst($str);
echo $str."<br>";
// note:只有第一个单词的首字母大写了哦
}
i want to become upper
I want to become upper
function funcUcwords() {
$str = "yld want to become upper";
echo $str."<br>";
$str = ucwords($str);
echo $str."<br>";
//note:每个单词的首字母都变成大写了哦
}
yld want to become upper
Yld Want To Become Upper
function funcAddslashes() {
//add slashes 添加反斜杠
$str = 'Hi Y"LD ';
$str = addslashes($str);
echo $str."<br>";
$str_one = "Hi Y'LD";
$str_one = addslashes($str_one);
echo $str_one."<br>";
//var_dump(get_magic_quotes_gpc($str_one));
//默认地,PHP 对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。
//所以您不应对已转义过的字符串使用 addslashes(),因为这样会导致双层转义。
//遇到这种情况时可以使用函数 get_magic_quotes_gpc() 进行检测。
var_dump(get_magic_quotes_gpc());
}
Hi Y\"LD
Hi Y\'LD
bool(false)
function funcStripslashes() {
//反引用一个引用字符串
$str = "Hi Y\'LD";
echo stripslashes($str);
}
Hi Y'LD
function funcExplode() {
$str = 'one|two||three|four';
// 默认输出
print_r(explode('|',$str));
// 正数的 limit note:three和four成为了同一个字符串
print_r(explode('|', $str, 4));
// 负数的 limit(自 PHP 5.1 起) note:four没有被输出
print_r(explode('|', $str, -1));
}
Array
(
[0] => one
[1] => two
[2] =>
[3] => three
[4] => four
)
Array
(
[0] => one
[1] => two
[2] =>
[3] => three|four
)
Array
(
[0] => one
[1] => two
[2] =>
[3] => three
)
function funcImplode() {
$arr = array('one-dimensional','array','values','can','be','converted','to','string');
$arr = implode(' ',$arr);
print_r($arr);
}
one-dimensional array values can be converted to string
function funcStrtok() {
$str = "I \nlove \tlaravel";
$tok = strtok($str,"\n\t");
while ($tok !== false) {
echo "Word=$tok<br />";
$tok = strtok(" \n\t");
}
}
Word=I
Word=love
Word=laravel
function funcSubstr() {
$str = 'abcdefg';
echo 'str: ';var_dump($str);
echo 'start=1: ';var_dump(substr($str,1));
echo 'start=1 length=-1: ';var_dump(substr($str,1,-1));
echo 'start=1 length=0: ';var_dump(substr($str,1,0));
echo 'start=1 length=2: ';var_dump(substr($str,1,2));
echo 'start=3 length=9>count($str): ';var_dump(substr($str,3,9));
echo 'start=8: ';var_dump(substr($str,8));
echo 'start=-3: ';var_dump(substr($str,-3));
echo 'start=-1 length=-4: ';var_dump(substr($str,-1,-4));
echo 'start=-3 length=2: ';var_dump(substr($str,-3,2));
}
str: string(7) "abcdefg"
start=1: string(6) "bcdefg"
start=1 length=-1: string(5) "bcdef"
start=1 length=0: string(0) ""
start=1 length=2: string(2) "bc"
start=3 length=9>count($str): string(4) "defg"
start=8: bool(false)
start=-3: string(3) "efg"
start=-1 length=-4: string(0) ""
start=-3 length=2: string(2) "ef"
function funcStrcmp() {
var_dump(strcmp('hi','hi'));
var_dump(strcmp('Hi','hi'));
var_dump(strcmp('hi','Hi'));
}
int(0)
int(-32)
int(32)
function funcStrcasecmp() {
var_dump(strcmp('hi','hi'));
var_dump(strcmp('hi','Hi'));
var_dump(strcmp('hi','Hh'));
}
int(0)
int(32)
int(32)
function funcStrnatcmp() {
$arr1 = $arr2 = array("img12.png", "img10.png", "img2.png", "img1.png");
echo "标准字符串比较\n";
usort($arr1, "strcmp");
print_r($arr1);
echo "\n自然秩序的字符串比较\n";
usort($arr2, "strnatcmp");
print_r($arr2);
}
标准字符串比较
Array
(
[0] => img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)
自然秩序的字符串比较
Array
(
[0] => img1.png
[1] => img2.png
[2] => img10.png
[3] => img12.png
)
function funcstrlen() {
$str = 'how long is my leg';
var_dump($str);
var_dump(strlen($str));
}
string(18) "how long is my leg"
int(18)
function funcStrstr()
{
$email = 'name@example.com';
$domain = strstr($email, '@');
var_dump($domain); // 打印 @example.com
$user = strstr($email, '@', true); // 从 PHP 5.3.0 起
var_dump($user); // 打印 name
}
string(12) "@example.com"
string(4) "name"
function funcStrrchr()
{
$path = '/www/public_html/index.html';
var_dump(strrchr($path, "/"));
$filename = substr(strrchr($path, "/"), 1);
var_dump($filename);
}
string(11) "/index.html"
string(10) "index.html"
function funcStrpos()
{
$string = "hello hello hello hello";
$find = "e";
// e的位置是1、7、13、19
var_dump(strpos($string, $find, 1));//从开始数,第1个位置开始查找
var_dump(strpos($string, $find, 3));//从开始数,第3个位置开始查找
var_dump(strpos($string, $find, 8));//从开始数,第8个位置开始查找
}
int(1)
int(7)
int(13)
function funcStrrpos()
{
$string = "hello hello hello hello";
$find = "e";
var_dump(strrpos($string, $find));
var_dump(strrpos($string, $find, -5));//从末尾数,第5个位置开始查找
var_dump(strrpos($string, $find, -23));//
}
int(19)
int(13)
bool(false)
function funcStrReplace()
{
//search replace 都为数组 且replace的值少于search的值
// 多余的替换会使用空字符
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer");
$newphrase = str_replace($healthy, $yummy, $phrase);
var_dump($newphrase);
//search replace 都为字符串
$str = str_replace("ll", "", "good golly miss molly!", $count);
var_dump($str);
var_dump($count);
// 输出 F ,因为 A 被 B 替换,B 又被 C 替换,以此类推...
// 由于从左到右依次替换,最终 E 被 F 替换
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
var_dump(str_replace($search, $replace, $subject));
// 输出: apearpearle pear
// 由于上面提到的原因
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
var_dump($output);
}
string(43) "You should eat pizza, beer, and every day."
string(18) "good goy miss moy!"
int(2)
string(1) "F"
string(16) "apearpearle pear"
function funcSubstrReplace()
{
$str = "Hello,YLD.Would you like eat something?";
$replace = "Hi";
var_dump(substr_replace($str,$replace,0));
var_dump(substr_replace($str,$replace,0,5));
var_dump(substr_replace($str,$replace,0,-7));
}
string(2) "Hi"
string(36) "Hi,YLD.Would you like eat something?"
string(9) "Hiething?"