PHP uses the Strpos function to mask the keyword program

Source: Internet
Author: User
Tags explode php code terminates

First look at the Strpos function

The Strpos () function returns the position where the string first appears in another string.

If the string is not found, it returns false.

Grammar

Strpos (string,find,start) parameter description
String required. Specify the string to be searched for.
Find required. Specify the characters to find.
Start optional. Specify where to start the search.

Tips and comments

Note: This function is sensitive to case sensitivity. Use the Stripos () function if you want to search for case insensitive.

Example

The code is as follows Copy Code

<?php
Echo Strpos ("Hello world!", "Wo");
?>

First, the key words written in a text file, each line one, the number of unlimited, how many write.

Second, PHP read the keyword text, stored in an array

Three, traverse the key word group, each use Strpos function to see the content has no keyword, if there is, return True, no return false


The PHP code is as follows

The code is as follows Copy Code

/**
* Use Strpos function in PHP to filter keywords
* Jones Taiwan Blog
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = Explode ("n", $content);
Traversal detection
For ($i =0, $k =count ($arr); $i < $k; $i + +) {
If this array element is empty, skip this loop
if ($arr [$i]== ') {
Continue
}

If a keyword is detected, it returns a matching keyword and terminates the run
if (@strpos ($str, $arr [$i])!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if the keyword is not detected
return false;
}


$content = ' Here is the text content to be published ... ';

Filter keywords
$keyWord = Keywordcheck ($content);

To determine if a keyword exists
if ($keyWord) {
Echo ' What you're posting there is a keyword '. $keyWord;
}else{
Echo ' Congratulations! through keyword detection ';
Down to the write library operation to complete the release action.
}

After writing the code, intentionally wrote a keyword content in the variable $content, and then ran the discovery did not detect the keyword, the execution result is passed, replaces the other prohibited keyword to pass.
Depressed, start to judge is not where the problem
Coding problems? Immediately open the KeyWord.txt file in Notepad again and save it as a UTF-8 format. The result is still not good.
Not getting keyWord.txt text content? Print_r () immediately discovers the normal read and turns the array into rows.
So, I put the key word group directly to the statement written dead in the program

The code is as follows Copy Code

<?php
/**
* Use Strpos function in PHP to filter keywords
* Jones Taiwan Blog
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = Explode ("n", $content);
Declare key word groups directly in your program
$arr = Array (' keyword 1 ', ' keyword 2 ', ' keyword 3 ', ' keyword 4 ' ...);
Traversal detection
For ($i =0, $k =count ($arr); $i < $k; $i + +) {
If this array element is empty, skip this loop
if ($arr [$i]== ') {
Continue
}

If a keyword is detected, it returns a matching keyword and terminates the run
if (@strpos ($str, $arr [$i])!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if the keyword is not detected
return false;
}

$content = ' Here is the content to be published, containing the keyword 2 ';
Filter keywords
$keyWord = Keywordcheck ($content);

To determine if a keyword exists
if ($keyWord) {
Echo ' What you have published is the keyword '. $keyWord. ' 】';
}else{
Echo ' Congratulations! through keyword detection ';
Down to the write library operation to complete the release action.
}
Program Run Result: You published the content of the keyword "key word 2"
Program Normal

If you declare a key word group in PHP, you can play a role, if you read the text file is not valid, hell?
At the time of baffled, think of the text file can be read from the contents of a space or newline characters do not filter caused? So we add a trim function to the traversal match.

Add the trim () function filtered blank after running through the test, the original blind toss a half-day problem right here.

The code is as follows Copy Code

/**
* Use Strpos function in PHP to filter keywords
* Jones Taiwan Blog
*/
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = Explode ("n", $content);
Traversal detection
For ($i =0, $k =count ($arr); $i < $k; $i + +) {
If this array element is empty, skip this loop
if ($arr [$i]== ') {
Continue
}

If a keyword is detected, it returns a matching keyword and terminates the run
This time, the trim () function is added
if (@strpos ($str, Trim ($arr [$i]))!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if the keyword is not detected
return false;
}


$content = ' Here is the text content to be published ... ';

Filter keywords
$keyWord = Keywordcheck ($content);

To determine if a keyword exists
if ($keyWord) {
Echo ' What you're posting there is a keyword '. $keyWord;
}else{
Echo ' Congratulations! through keyword detection ';
Down to the write library operation to complete the release action.
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.