Blogger Information
Blog 44
fans 0
comment 1
visits 30817
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4月18日作业——字符串函数
时光记忆的博客
Original
610 people have browsed it

实例

<?php
$siteName = 'php中文网';

echo strlen($siteName),'<br>';
echo mb_strlen($siteName);

/**
 * 字符串的比较运算
 */
echo "<p>字符串的比较运算</p>";

/**
 * 1.strcmp($str1, $str2);
 * 2.strncmp($str1, $str2, $str3)
 * 3.strcasecmp($str1, $str2):不区大小写
 * 4.strspan()
 * 5.strcspan()
 */

$str1 = 'php中文网';
$str2 = '溯源科技';

//1.strcmp()二个字符串相等:返回0.大于>0,否则<0
//
echo strcmp($str1, $str2) == 0 ? '相等' : '不相等';

//2.strncmp($str1, $str2, $length)比较开头部分指定长度是否相等
echo strncmp($str1, $str2, 5) == 0 ? '相等' : '不相等';

//3.strcasecmp($str1,$str2):不区分大小写

//strspan($str, $mark, $start, $length)
//计算字符串中全部都存在于在指定字符集合的第一段字符串的长度
echo strspn('15705519989', '0123456789'),'<br>';
echo strspn('15705519989', '0123456789',5,5),'<br>';
echo strspan('123453456343 45646465 2545645','0123456789'),'<br>';

<?php
echo '<p>字符串函数(三):字符串与数组之间的转换</p><hr color="red">';
/**
 * 1.str_split($str, $length)
 * 2.explode($delimiter, $str, $num)
 * 3.implode($glue, $str)
 */

// 1.str_split($str, $length)
echo '<pre>';
$str = 'word,excel,powter,nihao,hello';
// echo '<p>原始字符串:'.$str.'</p>';
// echo str_split($str, $length);
// print_r(str_split($str, 5));
// echo count(str_split($str));
// print_r(explode(',', $str));

//3.implode($glue, $str):按分割符,将一维数组
echo '<hr color="red">';
$arr2 = explode(',', $str);
print_r($arr2);
<?php
/**
 * 1.addslashes($str):单引号,双引号,反斜线,mysqli_real_escape_string()
 * 2.stripslashes($str):和上面是反操作
 * 3.htmlspecialchars()
 */

$str = <<< 'NOWDOC'
<h4>PHP课程介绍</h4>
<ol>
	 <li>ljkljkljl</li>
	 <li>ljkljkljl</li>
	 <li>ljkljkljl</li>
	 <li>ljkljkljl</li>
</ol>
NOWDOC;
//1.转义,存储避免代码注入
$str1 = addslashes($str);
echo $str1, '<hr>';

//2.stripslashes($str):和上面是反操作
$str2 = stripslashes($str1);
echo $str2,'<hr>';

$str3 = htmlspecialchars($str2);
echo $str3,'<hr>';

运行实例 »

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

165387859028823395.jpg

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