美文网首页
PHP字符串处理函数

PHP字符串处理函数

作者: TimeSHU | 来源:发表于2019-04-25 15:37 被阅读0次

字符串长度函数

//strlen-获取字符串长度
$str = 'time is yes';
echo strlen($str); //11-按字节算,空格也算。一个中文UTF-8是三个字节。GBK是两个字节

//mb_strlen-获取字符串长度
$str = 'time is yes';
$str1 = '中国';
echo mb_strlen($str);//11
echo "<hr/>";
echo mb_strlen($str1 , 'utf8');//2
echo "<hr/>";
echo mb_strlen($str1 , 'gbk');//3

//strpos-查找字符的首次出现位置,区分大小写
$str = 'time is yes TIME';
echo strpos($str , 'T');//12,查找首次出现的位置,区分大小写
echo strpos($str , 's');//6

echo "<hr/>";

//stripos-查找字符的首次出现位置,不区分大小写
$str = 'time is yes TIME';
echo stripos($str , 'T');//0,查找首次出现的位置,不区分大小写
echo stripos($str , 'I');//1

echo "<hr/>";

//strrpos-查找字符最后一次出现的位置,区分大小写
$str = 'time is yes TIMES';
echo strrpos($str , 's');//10

echo "<hr/>";

字符串替换函数

//str_replace-查找并替换字符串
$str = 'time is yes TIMES';
echo str_replace('yes' , 'no' , $str);//time is no TIMES
echo "<hr/>";
//str_ireplace-查找并替换字符串
echo str_ireplace('TIMES' , 'times' , $str);//time is yes times

echo "<hr/>";

//strtr-函数转换字符串中特定的字符
$str2 = '男孩,女孩,男人,女人';
echo $str2;//男孩,女孩,男人,女人
echo "<hr/>";
echo strtr($str2 , array('男'=>'女' , '女'=>'男'));//女孩,男孩,女人,男人

echo "<hr/>";

//substr_replace-替换字符串的子串
$str = 'time is yes TIMES';
echo substr_replace($str , 'nopapu' , 12);//time is yes nopapu

截取字符串函数

//substr-截取字符串
$str = 'time is yes TIMES';
echo substr($str , 7);//yes TIMES
echo "<hr/>";
echo substr($str , 7 , 4);//yes

echo "<hr/>";

//strstr-查找字符串的首次出现,截取,区分大小写
$str = 'time is yes TIMES';
echo strstr($str , 'is');//is yes TIMES

echo "<hr/>";

//stristr-查找字符串的首次出现,截取,不区分大小写
$str = 'time is yes TIMES';
echo stristr($str , 'TIME'); //time is yes TIMES

//strrchr-从最后一次搜索到的字符处返回
echo "<hr/>";
$str = 'time is yes TIMES';
echo strrchr($str , 's');//s TIMES

分割、连接、反转函数

//str_split-把$str 按 len 长度进行分割返回数组
$str = 'time is yes TIMES';
//把字符串以5为单位分割成数组单元
print_r(str_split($str , 5));//Array ( [0] => time [1] => is ye [2] => s TIM [3] => ES )

echo "<hr/>";

//explode-搜索字符串并取出,结果返回一个数组
$str = 'time is yes TIMES';
print_r(explode($str , 'is'));//Array ( [0] => is )
print_r(explode($str , 'TIME'));//Array ( [0] => TIME )

echo "<hr/>";

//implode-把数组值连接成字符串
$arr = array('d','b','q','i','l','y');
echo implode('' , $arr);//dbqily

空白处理函数

//trim-去除字符串首尾处的空白字符(或者其他字符)
echo "<hr/>";
$str = '\n\n\n\n\ntime is yes TIMES    ';
echo $str;//\n\n\n\n\ntime is yes TIMES
$s = '\n';
echo "<hr/>";
echo trim($str , $s);//time is yes TIMES

//ltrim-删除字符串开头的空白字符(或者其他字符)
$str = '\n\n\n\n\ntime is yes TIMES    ';
echo "<hr/>";
echo ltrim($str , $s);//time is yes TIMES

echo "<hr/>";

//rtrim-删除字符串末端的空白字符(或者其他字符)
$str = 'time is yes TIMES\n\n\n\n\n';
echo rtrim($str , $s);//time is yes TIMES

echo "<hr/>";

//chunk_split-向字符里面按N个字符就加入一个空格;
$str = 'timeisyesTIMES\n\n\n\n\n';
echo chunk_split($str , 4);//time isye sTIM ES\n \n\n \n\n


//str_pad
$st = 'time';
$str = ' is no';
echo "<hr/>";
echo str_pad($st , 10 , $str);//time is no
echo "<hr/>";
//默认在左边填充两个字节
echo str_pad($st , 10 , $str , STR_PAD_BOTH);//istime is
echo "<hr/>";
//将字符串全部放到左边
echo str_pad($st , 10 , $str , STR_PAD_LEFT);//is notime

字符转义函数

//addslashes-转义特殊字符
$str = "Is your name O'reilly?";
// 输出: Is your name O\'reilly?
echo addslashes($str);

echo "<hr/>";

//stripslashes-去掉转义字符
$str = "Is your name O\'reilly?";
// 输出: Is your name O'reilly?
echo stripslashes($str);

echo "<hr/>";

//htmlspecialchars-将特殊字符转换为 HTML 实体
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

echo "<hr/>";

//htmlspecialchars_decode —— htmlspecialchars()函数的反函数, 将 HTML 实体转换为字符
$new = "&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;";
echo htmlspecialchars_decode($new);//超链接test

echo "<hr/>";

//html_entity_decode —— htmlentities ()函数的反函数, 将 HTML 实体转换为字符
$new = "&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;";
echo html_entity_decode($new);//超链接test

echo "<hr/>";

//htmlentities —— 将字符串中所有可转换字符转换为 HTML 实体
$str = "A 'quote' is <b>bold</b>";
echo htmlentities($str);// A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo "<hr/>";
echo htmlentities($str, ENT_QUOTES);//A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;

字符串比较函数

//int strcmp($str1,$str2):(字符串比较)
$var1 = "Hello";
$var2 = "hello";
if (strcmp($var1, $var2) !== 0) {//区分大小写,var1和var2不相等
    echo "$var1 和 $var2 不相等";
}
//strcasecmp() 同上(不分大小写)
echo "<hr/>";
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {//因不区分大小写,var1和var2相等
    echo "$var1 和 $var2 相等";
}

相关文章

网友评论

      本文标题:PHP字符串处理函数

      本文链接:https://www.haomeiwen.com/subject/xyrzgqtx.html