PHP利用strpos函数屏蔽关键字程序
在我们做留言板时经常会需要去屏蔽关键字一些关键字,下面我来介绍利用txt保存要屏蔽关键字,然后再根据用户提交的数据进行过滤。
先看strpos函数
strpos() 函数返回字符串在另一个字符串中第一次出现的位置。
如果没有找到该字符串,则返回 false。
语法
strpos(string,find,start)参数 描述
string 必需。规定被搜索的字符串。
find 必需。规定要查找的字符。
start 可选。规定开始搜索的位置。
提示和注释
注释:该函数对大小写敏感。如需进行对大小写不敏感的搜索,请使用 stripos() 函数。
例子
代码如下 | 复制代码 |
echo strpos("Hello world!","wo"); |
一、把关键字专门写在一个文本文件里,每行一个,数量不限,有多少写多少。
二、PHP读取关键字文本,存入一个数组
三、遍历关键字数组,挨个用strpos函数去看看内容有没有关键字,如果有,返回true,没有则返回false
PHP代码如下
代码如下 | 复制代码 |
/**
|
写完代码后,故意在变量$content中写了一个关键字内容,然后运行发现没有检测到关键字,执行结果是通过,换成其它禁止的关键字都通过。
郁闷,开始判断是不是哪里出问题
编码问题? 立即把keyWord.txt文件再次用记事本打开,然后另存为UTF-8格式。结果还是不行。
没有获取到keyWord.txt文本内容? 立即 print_r() 发现正常读取并按行转成了数组。
于是,我把关键字数组直接声明写成死的在程序中
代码如下 | 复制代码 |
/** |
如果在PHP中声明关键字数组,就能起到作用,如果读取文本文件就无效,见鬼了?
正在百思不得其解之时,想到了会不会是从文本文件中读取的内容有空格或者换行符没有过滤所导致?于是在遍历匹配那里加了一个trim函数
加了trim()函数过滤空白后运行通过测试,原来瞎折腾了半天问题就在这里。
代码如下 | 复制代码 |
/** |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
