php——学习笔记,检索字符串和它的次数,替换字符串

来源:互联网 发布:idea java code style 编辑:程序博客网 时间:2024/05/26 02:55
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>


<body>
<?php
echo strstr("the world may change all life through","h");
?>
</body>

</html>

所以strstr函数的功能是在母串里找到子串,第一次找到以后直接从找到的位置开始输出到母串的最后


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>


<body>
<?php
echo substr_count("the world may change all life through","h");
?>
</body>
</html>

统计子串在母串中出现的次数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>


<body>
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";


?>
</body>
</html>

str_replace("要被替换的子串","替换成什么子串","从哪个母串里替换",用于统计替换的次数)且区分大小写,str_ireplace不区分大小写



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>应用substr_replace()函数对指定字符串进行替换</title>
</head>


<body>
<?php
$str="用今日的辛勤工作,换明日的双倍回报!"; //定义字符串常量
$replace="超级无敌一百分"; //定义预替换的字符串``
echo substr_replace($str,$replace,26,6);   //替换字符串
?>
</body>
</html>

substr_replace(母串,子串,n,【可选参数m】)

从母串的第(n+1)的字符开始的长为m个字符的串替换成子串


0 0
原创粉丝点击