Explanation of the main functions of the PHP message module (code can be implemented)

Source: Internet
Author: User
This article provides a detailed analysis of the main functions and codes in the PHP message book. For more information, see I. sensitive word processing
1. filter sensitive words
The preg_match () function is used to search for all the content that matches the given regular expression in the string. if so, True is returned. otherwise, False is returned.

Syntax:
Int preg_match (string pattern, string subject [, array matches [, int flags])
Parameters of the preg_match () function are described as follows:
Pattern:
Required parameter, the regular expression to be matched
Subject:Required parameter, input string
Matches:Optional. The array that outputs the search results. for example, $ out [0] will contain the results that match the entire pattern, $ out [1] will contain results matching the child pattern in the first captured bracket, and so on
Flags:Optional. Mark: PREG_OFFSET_CAPTURE. the offset of the affiliated string is returned for each matching result page, which is available after PHP4.3.0.

The specific implementation method is as follows:
First, use the file () function to read and store the sensitive words in the sub-text file (each sensitive word is separated into one line), and store them in the array $ file_word.
Then, the for loop statement is applied to automatically read array elements (sensitive words), and the regular expression is used to check whether the message submitted by the user contains sensitive words.

When a user sends a message and submits Li Yan information, the message is compared with the sensitive words stored in the array. if the message contains sensitive words, a prompt is displayed, otherwise, the message is published successfully. The key code for sensitive word filtering is as follows:

The code is as follows:


If (is_file ("./filterwords.txt") {// Determine whether the given file name is a normal file
$ Filter_word = file ("filterwords.txt"); // read the entire file into an array.
$ Str = $ _ POST ['content'];
For ($ I = 0; $ I If (preg_match ("/". trim ($ filter_word [$ I]). "/I", $ str) {// use a regular expression to determine whether the sent message contains sensitive words.
Echo "script" alert ('the message contains sensitive words! '); History. back (-1); script ";
Exit;
}
}
}


File () function
Array file (string filename [, int use_include_path [, resource context])
Like readfile (), only file () is returned as an array. Each unit in the array is a corresponding line in the file, including line breaks. If file () fails, FALSE is returned.
2. add sensitive words to text files.
Main functions of php to write sensitive words into text files
(1) is_writable () function
The is_writable () function is used to determine whether a file exists and is writable. if the preceding conditions are met, True is returned; otherwise, False is returned.
Syntax: The bool is_writable (string filename) parameter filename is used to specify the full path of the file (c:/leaveMessage/filterwords.txt#or relative values (the file should be called using filterwords.txt). it can return true if not written.
Example:

The code is as follows:


If (is_writable ("filterwords.txt ")){
Echo "writable files ";
} Else {
Echo "file not writable ";
}


(2) fopen () function
The fopen () function is used to open a file and return the pointer to the file. The file can be local or remote.
Syntax: resource fopen (string filename, string mode [, int use_include_path [, resource context])
Parameters:
Filename:
Required parameter. Used to specify the local or remote address of the file to be opened
Mode:Required parameter. Used to specify the mode of the file to be opened use_include_path: optional parameter. If this parameter is set to True, PHP will try to open the file according to each point in the include_path standard.
Context: an optional parameter. Set some options to improve File Stream performance

Mode parameter description:
'R' is opened in read-only mode. it directs the file pointer to the file header.
Open in 'R + 'read/write mode and point the file pointer to the file header.
Open in 'W' writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
Open in 'W + 'read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.
Open the 'A' write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
Open in 'A + 'read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. 'X' is created and opened in writing mode. it directs the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) system call. This option is supported by PHP4.3.2 and later versions and can only be used for local files. 'X + 'is created and opened in read/write mode. the file pointer points to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files.

Example: the fopen () function opens a file in read-only mode.

The code is as follows:


$ Fp = fopen ("filterwords.txt", "r ");
?>


(3) fseek () function
The fseek () function is used to set the position of the file pointer.
Syntax: int fseek (resource handle, int offset [, int whence])
Parameters:
Handle:
Required parameter. The identifier of the file returned when a file is opened
Offset:Required parameter. Used to set the position of the file pointer
Whence:Save parameters.
Fessk ()Description of The whence parameter of the function
Seek_set:Set the position to be equal to offset bytes. This value is the default value of this parameter.
Seek_cur:Set the position to equal to the current position plus offset bytes
Seek_end:Set the position to equal to the end of the file plus offset bytes
For example, use fopen(‑test.txt "to open the file named ‑test.txt, read the file's 4096-byte content, and then use the fseek () function to restore the file pointer to its original location.

The code is as follows:


$ Fp = fopen ("filterwords.txt", "r"); // open a file in read-only mode
$ Data = fgets ($ fp, 4096); // read a row of 4096 bytes in the file
Fseek ($ fp, 0); // point the file pointer to the location of 0th bytes
?>


String fgets (int handle [, int length])
Read a row from the file pointed to by handle and return a string of up to length-1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes. (Considering the program execution efficiency, a fixed length value is usually used)
(4) fwrite () function
The fwrite () function is used to write a string to a specified file and specify the size of the written bytes.
Syntax: int fwrite (resource handle, string [, int length])
Parameter description:
Handle:
Required parameter. File ID pointer
String:Required parameter. String of the file to be written
Length:Save parameters. The length of the file to be written. If this parameter is omitted, all content of the specified string will be written to the file.
For example, fwrite(mongoshake writes a string of two phpmessage headers to the filterwords.txt file.

The code is as follows:


$ Fp = fopen ("filterwords.txt", "w + ");
$ Str = "PHP message book ";
If (fwrite ($ fp, $ str )){
Echo "file written successfully! ";
} Else {
Echo "file writing failed ";
}
?>


(5) fclose () function
The fclose () function is used to close the file indicated by the specified file ID.
Syntax: bool fclose (resource handle)
The handle parameter is the identifier of the file returned after the fopen () function or fsockopen () function successfully opens a file.
In the message book, you can add sensitive words to the internal file named filterwords.txt.

The code is as follows:


If ($ _ POST ){
$ Filename = "../filterwords.txt ";
If (is_writable ($ filename )){
$ File = fopen ($ filename, 'R + '); // open the file in read/write mode and point the file pointer to the file header.
}
Else {
Echo "file". $ filename. "cannot be written ";
}
// Write the file at the end of the file
Fseek ($ file, 0, SEEK_END); // you can specify the pointer position. SEEK_END indicates that the position is equal to the end of the file and the offset (here 0) bytes are added.
$ Word = $ _ POST [txt_word];
Fwrite ($ file, $ word );
Fwrite ($ file, "\ r \ n ");
Fclose ($ file );
}
?>


3. read sensitive words in the file

The code is as follows:


$ Filename = "../filterwords.txt ";
If (is_readable ($ filename )){
$ Arr = file ($ filename );
}
Else {
Echo "file". $ filename. "unreadable ";
}
While (list ($ name, $ value) = each ($ arr) {// traverses the array
$ A. = "$ value ".",";
}
Echo"
Sensitive words:
". $;
?>


(1) is_readable () checks whether the file exists and is readable. if the preceding conditions are met, True is returned.
Syntax: bool is_readable (string filename). The filename parameter is used to specify the complete path of the file.
(2) void list (mixed ...)
Like array (), this is not a real function, but a language structure. List () assigns values to a group of variables with one-step operations. List () can only be used as an array of numeric indexes. it is assumed that the numeric index starts from 0.
For example:

The code is as follows:


$ Info = array ('Coffee ', 'Brown', 'Caffeine ');
List ($ drink, $ color, $ power) = $ info;
Print "$ drink is $ color and $ power makes it special. \ n ";
// Coffee is brown and caffeine makes it special.
List ($ drink, $ color) = $ info;
Print "$ drink is $ color. \ n ";
// Coffee is brown.
?>


(3) array each (array)
Returns the keys and values of the current pointer position in the array and moves the array pointer forward. The key-value pair is returned as an array of four units. the key names are 0, 1, key and value. unit 0, and key contain the key names of the array units, and 1 and value contain data. If the internal pointer crosses the end of the array, each () returns FALSE.

The code is as follows:


$ Foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese ");
$ Bar = each ($ foo );
Print_r ($ bar );
?>


The returned array of each () is as follows:
Array
{
[1] => bob
[Value] => bob
[0] => 0
[Key] => 0
}
List () only applies to numeric indexes, and the default key starts from scratch. Therefore, list ($ name, $ value) assigns the keys 0 and bob to $ name and $ value respectively.
4. Select All and invert check boxes.
The number of check boxes dynamically created based on the application of for loop statements in the database. the names of all or reversed check boxes must be set to note_id []. The value of the check boxes is the ID of the message.


Add a all-selected check box as the content to be selected. When this check box is selected, the user-defined function check_all () is called to set all the check boxes.


Select all
Select all, traverse all the check boxes form. elements [I], and set the checked attribute of each multiple options to True.
Returns the list of all check boxes in form. elements [I] to obtain the checked attribute value of the check box. if it is True, it is set to False. Otherwise, it is set to True, which is the opposite of the current value.

The code is as follows:


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.