PHP中检索子串出现的次数

一 代码

<?php
{
	$user=$_POST[user];
	$text=$_POST[text];
	if(substr_count($user,'大人')>0 or substr_count($text,'大人')>0 or substr_count($user,'小人')>0 or substr_count($text,'小人')>0)
	{
		echo "<script>alert('您输入的内容中有指定的关键字'); history.back();</script>";
	}
}
?> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>substr_count()函数的应用</title>
<style type="text/css">
<!--
.STYLE1 {
	font-size: 13px;
	font-weight: bold;
}
-->
</style>
</head>
<body>
<table width="720" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td></td>
  </tr>
</table>
<table width="720" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="121"></td>
    <td width="584" height="454"><table width="584" height="454" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td align="center" valign="top" bgcolor="#F2F7FB">
         <form name="form1" method="post" action="index.php">
         <table width="580" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="89" height="30">&nbsp;</td>
            <td width="491">&nbsp;</td>
          </tr>
          <tr>
            <td align="right"><span class="STYLE1">名称:&nbsp;</span></td>
            <td height="24"><input name="user" type="text" id="user" size="50" /></td>
          </tr>
          <tr>
            <td align="right"><span class="STYLE1">内容:&nbsp;</span></td>
            <td><textarea name="text" cols="60" rows="10" id="text"></textarea></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><input type="submit" name="Submit" id="Submit" value="提交"></td>
            </tr>
        </table>
        </form>        
       </td>
      </tr>
      <tr>
        <td height="48"></td>
      </tr>
    </table></td>
    <td width="15"></td>
  </tr>
</table>
</body>
</html>

 

二 运行结果
三 函数
int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
substr_count() 返回子字符串needle 在字符串 haystack 中出现的次数。注意 needle 区分大小写。 
Note: 
该函数不会计算重叠字符串。参见下面的例子。 
参数 
haystack
在此字符串中进行搜索。 
needle
要搜索的字符串。 
offset
开始计数的偏移位置。 
length
指定偏移位置之后的最大搜索长度。如果偏移量加上这个长度的和大于 haystack 的总长度,则打印警告信息。 
返回值 
该函数返回整型。
  • 大小: 7.9 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值