Blogger Information
Blog 22
fans 1
comment 1
visits 22668
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
常用字符串函数
forever浅笑
Original
709 people have browsed it

实例

<?php
header("content-type:text/html;charset=utf-8");
/**
 * 获取字符串的长度
 */
echo '<h3>strlen($str)</h3>';
$str = 'abcdef';
$str2 = '中国人';
echo $str . ' 的长度为' . strlen($str), "<br><br>";
echo $str2 . ' 的长度为' . strlen($str2);
echo '<hr>';

/**
 *  获取内部字符编码集
 */
echo '<h3>mb_internal_encoding()</h3>';
$encoding = mb_internal_encoding();
echo "内部字符编码为{$encoding}";
echo '<hr>';

/**
 * 获取字符串长度
 */
echo '<h3>mb_strlen($str,"utf-8")</h3>';
$str = '中国人';
echo $str . ' 的长度为' . mb_strlen($str, 'utf-8');
echo '<hr>';

/**
 *  字符串比较
 */
$str1 = 'hello';
$str2 = 'hello';
echo '<h3>strcmp($str1,$str2)</h3>';
echo strcmp($str1, $str2) == 0 ? '相等' : '不相等';

echo '<h3>strncmp($str1,$str2,length)</h3>';  // 比较str1和str2前几位的长度的字符是否相等
echo strncmp($str1, $str2, 3) == 0 ? '相等' : '不相等';

echo '<h3>strcasecmp($str1,$str2[,length])</h3>';     // 比较str1和str2不区分大小写是否相等
$str1 = 'Hello';
$str1 = 'hello';
echo strcasecmp($str1,$str2) ? '相等' : '不相等';

echo '<h3>strspn($str, $mark[, $start, $length])</h3>';   // 计算字符串中的全部字符都存在于指定字符集合的第一段子串的长度
echo strspn('15447854789', '0123456789'),'<br>';
echo strspn('15447854789', '0123456789',4,4),'<br>';
echo strspn('15447854789 458987 1125478', '0123456789');

/**
 *  字符串转为数组
 */
 echo '<h3>explode($delimit,$str)</h3>';
 $str = '1,2,3,4,5,6';
 echo '<pre>';
 print_r(explode(',',$str));
 echo '<hr>';

/**
 *  数组转为字符串
 */
echo '<h3>implode($glue,$arr) / join($glue,$arr)</h3>';
$arr = [1,2,3,4,5];
echo implode(',',$arr);
echo '<hr>';

/**
 *  字符串替换
 */
echo '<h3>str_replace($search,$replace,$str)</h3>';
$str = '中华人民共和国';
echo str_replace('国','国万岁',$str);

/**
 * 字符串查找
 */
echo '<h3>strpos($str,$needle)</h3>';
$str = '中华人民共和国';
echo strpos($str,'国');
echo '<hr>';

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!