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 的总长度,则打印警告信息。 
返回值 
该函数返回整型。

猜你喜欢

转载自cakin24.iteye.com/blog/2350835
今日推荐