4.1. 字符串常用函数

未匹配的标注

持之以恒,方得始终!

trim(), ltrim(), rtrim()

去除字符串左右两边的空格,或其它字符。
比如我们获取的数据要入库,或者和其它字符串做比较。

$name = trim($_REQUEST['name']);

nl2br()

用html中的 <br/> 替换字符串中的 \n

echo nl2br($mailcontent);

echo, print(), printf(), sprintf(), vprintf(), vsprintf()

echo 输出,可以看作是一个语法结构,语法规定就是这么用的,不必深究。
print() 也是输出,和echo一样,不过它有返回值。
printf() 可以实现一些复杂的格式,也是直接输出。
sprintf() 和printf()一样,不过它不是直接输出了,是返回一个格式化后的字符串。

$total = 12.4;


echo "Total amount of order is $total. \n";

printf("Total amount of order is %s. \n", $total);

// Total amount of order is 12.4.
// Total amount of order is 12.4.

printf("Total amount of order is %.2f \n", $total); // Total amount of order is 12.40

$total_shipping = 100;
printf("Total amount of order is %.2f (with shipping %.2f)", $total, $total_shipping); // Total amount of order is 12.40 (with shipping 100.00)

%s 表示这里需要用一个字符串来替换,从后面的参数中按所在顺序位置找。
%.2f 表示这里需要一个精确到小数点后两位的浮点数来替换。
具体怎么用,我们看手册即可。常用的就是%d, %s, %f

替换的参数,我们还可以手动指定,%2$,使用第二个参数。

$total = 12.4;

$total_shipping = 100;

printf("Total amount of order is %2$.2f (with shipping %1$.2f)", $total, $total_shipping);

//Total amount of order is 100.00 (with shipping 12.40)

比如有参数重复情况下,就可以这样用了。

vprintf(), vsprintf() 第二个参数是接收数组,具体看手册。

strtoupper(), strtolower(), ucfirst(), ucwords()

strtoupper() 将字符串全部转为大写。
strtolower() 将字符串全部转为小写。
ucfirst() 将第一个字符,转为大写。
ucwords() 将字符串中的每个单词的首字母,转为大写。

addslashes(),stripslashes()

某些特殊字符,比如' " \ \\ null等等,如果不处理,直接入库,则会出现一些问题。
我们可以加转义字符\, 即变成 \' \" \\ \\\\ \null

$feedback = "tihs's very'goods\"idea"; // 一般是外部输入
$feedback = addslashes(trim($feedback));
print_r($feedback); // tihs\'s very\'goods\"idea

如果我们的\多了,那么可能是php配置中的 magic_quotes_gpc 开启了。gpc表示 get,post,cookie。开启后,会自动加\
我们可以用 get_magic_quotes_gpc() 函数,检查是否启用了。

stripslashes() 是移除\, 比如我们输出到html中,就需要移除了。

一般我们建议根据DB,选择其自己的转义函数,比如mysql的mysqli_real_escape_string()

explode(),implode()

explode() 将字符串按指定的分隔符,分割为一个数组。
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
第三个参数,表示分割的数量。

$email = "hello@163.com";
$email_arr = explode('@', $email);
print_r($email_arr);
/* Array
(
    [0] => hello
    [1] => 163.com
) */

if(strtolower($email_arr[1]) == 'bigcustomer.com') { // 看到没,这里就把接收进来的字符串转为小写,因为我们不知道用户输入的情况。
    $toaddress = "bob@example.com";
} else {
    $toaddress = 'feedback@example.com';
}

implode() 将数组中的值,按指定的符号,组合为一个字符串。

$arr = ['bob','example.com'];
$str = implode('@', $arr);
print_r($str); // bob@example.com

strtok()

也是分割字符串,和explode()一次全部分割好不一样,strtok()是一次只从字符串中取出一个片段。

$string = "This is\tan example\nstring";

$tok = strtok($string, " \n\t");

while ($tok !== false) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}

substr()

从字符串中截取部分。直接看例子:

$test = "your customer service is excellent";

echo substr($test, 1) . PHP_EOL; // our customer service is excellent

echo substr($test, -9) . PHP_EOL; // excellent

echo substr($test, 0, 4) . PHP_EOL; // your

echo substr($test, 5, -13); // customer service

对于有中文的截取,我们可以用 mb_substr()

字符串的排序比较

strcmp(), strcasecmp(), strnatcmp(), strnatcasecmp()
strcmp($str1, $str2), 如果两个字符串相等,返回0;如果按字典顺序,str1在str2后面,即str1大于str2,返回正数。如果str1在str2前面,即str1小于str2 ,返回负数。这个函数区分大小写。

$str1 = "Hello";

$str2 = "hello";

var_dump(strcmp($str1, $str2)); // H < h , 即 $str1 < $str2, 返回 -1

strcasecmp() 和 strcmp()一样,但是不区分大小写。

$str1 = "Hello";
$str2 = "hello";

var_dump(strcasecmp($str1, $str2)); // int(0)

strnatcmp(), strnatcasecmp() 是按自然顺序对比排序的,比如 '2'是排序大于'12', 因为 '2' 大于 '1'; 自然排序,则是按人的习惯, '2' < '12'

var_dump(strnatcmp(2, 12)); // int(-1)

strlen()

返回字符串的长度

if(strlen($email) < 6) {
    echo "email error";
}

匹配 strstr(),stristr(),strrchr()

比如,我要在反馈文本中,搜索shop字符串,可以用 explode(), strtok(), 在文本中检索每个单词,然后用 ==, 或 strcmp() 进行比较。当然下面的函数更好用。

strstr()

$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // @example.com

$toaddress = "feedback@example.com";
if(strstr($feedback, 'shop')) {
 $toaddress = "retail@example.com";
} else  if(strstr($feedback, 'delivery')) {
 $toaddress = 'fulfillment@example.com';
}

stristr() 和strstr()一样,但不区分大小写。
strrchr() 和 strstr() 一样,但是,是从最后匹配到地方开始算。

$email  = 'name@example.com@123';

$domain = strrchr($email, '@');

echo $domain; // @123

这三个函数,匹配到后,都是返回剩余字符串。

查找子字符串的位置 strpos(),strrpos()

strpos() 返回第一次匹配到的位置

$test = 'Hello world ';
echo strpos($test, "o"); // 4
echo strpos($test, "o", 5); // 7, 第三个参数5,表示从位置5开始搜索

strrpos() 返回最后一次匹配到的位置

$test = 'Hello worlod ';
echo strrpos($test, "o"); // 10

如果匹配不到,它们都返回false,但是假如匹配到的位置是0,那么 false == 0,是对的,这样判断就有问题了,我们可以用 ===

$test = 'Hello worlod ';

$res = strpos($test, "H");

if($res === false) {
    echo "没有搜索到目标字符";
} else {
    echo "找到了,位于:" . $res;
}

替换 str_replace(),substr_replace()

处理字符串的一些常用函数
搜索第一参数,用第二个参数替换之,第三个参数是给的字符串,第四个参数是被替换的次数。

$feedback = str_replace($offcolor, '%!@*', $feedback);

substr_replace() 选择字符串中的某一段进行查找替换。

$test = "hello world";

// 用 X 替换最后一个字符
$test = substr_replace($test, 'X', -1);
echo $test; // hello worlX

还有第四个参数,可选的长度length,如果不给
处理字符串的一些常用函数
如果 length 为0
处理字符串的一些常用函数
如果是正数
处理字符串的一些常用函数
如果是负数
处理字符串的一些常用函数

如有任何侵权行为,请通知我删除,谢谢大家!
个人邮箱:865460609@qq.com

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
Junwind
讨论数量: 0
发起讨论 只看当前版本


暂无话题~