悠悠楠杉
php常用字符串函数
11/13
在头部添加以下代码,以防乱码:
header("content-type: text/html; charset=utf-8");
strlen/mb_strlen 获取字符串长度
$str = '至尊';
echo strlen($str);//6 获取字符串长度,utf8一个中文字符占3个字节,gbk一个中文字符占2个字符
echo mb_strlen($str,'utf8');//2 根据编码获取字符串长度
strpos 查找字符串首次出现的位置
$str = 'abcdef';
echo strpos($str,'c');//2
if(strpos($str,'c') !== false){
echo 'yes';
}
stripos 查找字符串首次出现的位置(不区分大小写)
$str = 'abcdef';
echo stripos($str,'c');//2
if(stripos($str,'c') !== false){
echo 'yes';
}
strrpos 查找字符串中最后一次出现的位置
$foo = "0123456789a123456789b123456789c";
var_dump(strrpos($foo, '7'));// 结果: int(27)
strripos 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
$foo = "0123456789a123456789b123456789c";
var_dump(strripos($foo, '7'));// 结果: int(27)
str_replace 字符串替换
$str = 'www.qq.com';
echo str_replace('w.qq.c','*',$str);//ww*om
strtr 转换指定字符
$str = '男人,女人,男孩,女孩';
echo strtr($str,array('男' => '女','女' => '男'));//女人,男人,女孩,男孩
substr 截取字符串
$str = 'helloword';
echo substr($str,2).'<br>';//lloword
echo substr($str,2,3);//llo
explode 拆分字符串
$str = 'linux,php,mysql';
print_r(explode(',',$str));//Array ( [0] => linux [1] => php [2] => mysql )
implode 将数组的值拼接成字符串
$arr = array('name' => 'zhangsan','age' => '21','gender' => 'man');
echo implode('/',$arr);// zhangsan/21/man
strstr 查找字符串的首次出现
$email = 'name@example.com' ;
$domain = strstr ( $email , '@' );
echo $domain; // 打印 @example.com
strrchr 查找指定字符在字符串中的最后一次出现
echo strrchr("Hello world! What a beautiful day!",'What');//What a beautiful day!
获取.后面的后缀名
$str = 'a.jpg';
echo substr($str, strpos($str,'.')+1);//jpg
echo '<br>';
echo strrchr($str,'.');//.jpg 查找字符串最后出现的位置
echo '<br>';
echo ltrim(strrchr($str,'.'),'.');//jpg 删除字符串开头的空白字符或者其他字符。rtrim()是删除末端的
http_build_query 生成 URL-encode 之后的请求字符串
<?php
$data = array( 'foo' , 'bar' , 'baz' , 'boom' , 'cow' => 'milk' , 'php' => 'hypertext processor' );
echo http_build_query ( $data ) . "\n" ;
echo http_build_query ( $data , 'myvar_' );
?>
以上例程会输出:
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor
myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor
str_shuffle 随机打乱一个字符串
<?php
$str = 'abcdef' ;
$shuffled = str_shuffle ( $str );
// 输出类似于: bfdaec
echo $shuffled ;
?>
htmlspecialchars 把预定义的字符 "<" (小于)和 ">" (大于)转换为 HTML 实体
<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>
htmlspecialchars_decode 将特殊的 HTML 实体转换回普通字符
<?php
$str = "<p>this -> "</p>\n" ;
echo htmlspecialchars_decode ( $str );
// 注意,这里的引号不会被转换
echo htmlspecialchars_decode ( $str , ENT_NOQUOTES );
?>
mb_substr 获取字符串的部分
<?php
echo mb_substr("至尊技术网", 0, 2);// 至尊
?>
strtolower 将字符串转化为小写
$str = "Mary Had A Little Lamb and She LOVED It So" ;
$str = strtolower ( $str );
echo $str ; // 打印 mary had a little lamb and she loved it so
strtoupper 将字符串转化为大写
$str = "Mary Had A Little Lamb and She LOVED It So" ;
$str = strtoupper ( $str );
echo $str ; // 打印 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
ucwords 将字符串中每个单词的首字母转换为大写
$foo = 'hello world!' ;
$foo = ucwords ( $foo ); // Hello World!
$bar = 'HELLO WORLD!' ;
$bar = ucwords ( $bar ); // HELLO WORLD!
$bar = ucwords ( strtolower ( $bar )); // Hello World!
ucfirst 将字符串的首字母转换为大写
$foo = 'hello world!' ;
$foo = ucfirst ( $foo ); // Hello world!
$bar = 'HELLO WORLD!' ;
$bar = ucfirst ( $bar ); // HELLO WORLD!
$bar = ucfirst ( strtolower ( $bar )); // Hello world!
PHP界定符 <<<EOT 字符串的特殊输入方式,也叫格式输入
$config = <<<EOT
<?php
return [
'mysql' => [
// 数据库类型
'type' => Env::get('database.type', 'mysql'),
// 服务器地址
'hostname' => Env::get('database.hostname', '{$data['hostname']}'),
// 数据库名
'database' => Env::get('database.database', '{$data['database']}'),
// 用户名
'username' => Env::get('database.username', '{$data['username']}'),
// 密码
'password' => Env::get('database.password', '{$data['password']}'),
// 端口
'hostport' => Env::get('database.hostport', '{$data['hostport']}'),
],
EOT;
iconv 字符串按要求的字符编码来转换
$text = "This is the Euro symbol '€'.";
echo 'Original : ', $text, PHP_EOL;
echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;
echo 'IGNORE : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;
echo 'Plain : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;
gethostbyname 返回主机名对应的 IPv4地址
echo gethostbyname('baidu.com');
生成随机字符串
function rand_str($num = 6,$special = false)
{
$str = 'abcedfghjkmnpqrstuvwxyzABCEDFGHJKMNPQRSTUVWXYZ0123456789';
if($special){
$str .= '!@#$%^&*';
}
return substr(str_shuffle($str), 0, $num);
}