Detailed explanation of PHP regularity and data collection
PHP regular expressions are mainly used for pattern segmentation, matching, search and replacement operations of strings. Using regular expressions may not be efficient in some simple environments,
Therefore, how to better use PHP regular expressions needs to be considered comprehensively.
Definition of PHP regular expression:
A grammatical rule used to describe character arrangement and matching patterns.
Regular functions in PHP:
There are two sets of regular functions in PHP, both of which have similar functions:
One set is provided by the PCRE (Perl Compatible Regular Expression) library. Functions named with the prefix "preg_";
A set provided by POSIX (Portable Operating System Interface of Unix) extensions. Use functions named with the prefix "ereg_";
(POSIX regular function library is no longer recommended for use since PHP 5.3, and will be removed from PHP 6)
Since POSIX regular function library is about to be released, history Stage, and the forms of PCRE and perl are similar, which is more convenient for us to switch between perl and php, so here we focus on the use of PCRE regularity.
PCRE Regular Expression
PCRE stands for Perl Compatible Regular Expression, which means Perl compatible regular expression.
In PCRE, the pattern expression (ie regular expression) is usually included between two backslashes "/", such as "/apple/".
Several important concepts in regular expressions include: metacharacters, escapes, pattern units (repetitions), antonyms, references, and assertions. These concepts can be easily understood and mastered in JavaScript.
preg_filter — Regular expression search and replacement
preg_filter("regular", "replacement", "target");
preg_grep ( string $pattern , array $input [, int $flags = 0 ] )
preg_match — Perform a regular expression match
preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
preg_match_all — Global regular expression matching
preg_replace — Perform a regular expression search and replace
preg_split — Separate strings by a regular expression
Commonly used meta-characters:
Metacharacter Description
\A Matches the atom at the beginning of the string
\Z Matches the atom at the end of the string
\b Matches the boundary of the word /\bis/ Matches the string whose head is is /is\ b/ Matches strings ending in is /\bis\b/ Delimitation
\B Matches any character except word boundaries /\Bis/ Matches "is" in the word "This"
\d Matches a number; equivalent to [0-9]
\D Matches any character except numbers; equivalent to [^0-9]
\w Matches an English letter, number or underscore; equivalent In [0-9a-zA-Z_]
\W Matches any character except English letters, numbers and underscores; equivalent to [^0-9a-zA-Z_]
\s Matches a blank character ; Equivalent to [\f\t\v]
\S Matches any character except whitespace characters; Equivalent to [^\f\t\v]
\f Matches a form feed character Matches a newline character in \x0c or \cL
; matches a carriage return character in \x0a or \cJ
; matches a tab character in \x0d or \cM
\t ; etc. Equivalent to \x09\ or \cl
\v Matches a vertical tab character; Equivalent to \x0b or \ck
\oNN Matches an octal digit
\xNN Matches a hexadecimal digit
\cC Matches a control character
Pattern Modifiers:
Pattern modifiers are particularly used in ignoring case and matching multiple lines. Mastering this modifier can often solve our problems. Got a lot of questions.
i ——Can match both upper and lower case letters
M ——Treat the string as multiple lines
S ——Treat the string as a single line, and treat newlines as ordinary characters, so that "." matches any character
” etc., ignoring case. /i
Matches
characters Description
\ Marks the next character as a special character, text, backreference, or octal escape character. For example, "n" matches the character "n". "\n" matches a newline character. The sequence "\\" matches "\", and "\(" matches "(".
^ Matches the beginning of the input string. If the Multiline property of the RegExp object is set, ^ will also match "\n" or " Matches the position after \r".
$ Matches the end of the input string. If the RegExp object's Multiline property is set, $ also matches the position before "\n" or "\r".
* Matches the preceding character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * Equivalent to {0,}.
+ Matches the previous character or subexpression one or more times. For example, "zo+" matches "zo" and "zoo" but not "z". + Equivalent to {1,}.
? Matches the preceding character or subexpression zero or once times. For example, "do(es)?" matches the "do" in "do" or "does". ? Equivalent to {0,1}.
{n} n is a non-negative integer. Matches exactly n times. For example, "o{2}" does not match the "o" in "Bob" but does match both "o"s in "food".
{n,} n is a non-negative integer. Match at least n times. For example, "o{2,}" does not match the "o" in "Bob" but matches all o's in "foooood". "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{n,m} M and n are non-negative integers, where n ? When this character immediately follows any other qualifier (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is “non-greedy”. The "non-greedy" pattern matches the shortest possible string that is searched for, while the default "greedy" pattern matches the longest possible string that is searched for. For example, in the string "oooo", "o+?" matches only a single "o", while "o+" matches all "o"s.
. Matches any single character except "\n". To match any character including "\n", use a pattern such as "[\s\S]".
[] Matches any one in []
[^] Matches any one not in []
1. What is data collection
A few years ago, except for a few large portals, Basically they are personal websites. The information is scattered and there is not much content.
In a few years, there will be more and more commercial websites, and information needs to be concentrated in large quantities. Even if there are enough financial resources to hire a large number of copy editors,
may not be able to meet the ever-changing information resources.
Information collection has become a technology favored by all website operators.
2. The idea of data collection:
The idea of the collection program is very simple and can be roughly divided into the following steps:
1. Obtain the remote file source code (file_get_contents or use fopen or use fsocket to implement the http protocol get, or PHP's curl extension or PHP's other third-party open source classes)
Note: It is best not to use file_get_contents and fopen, you will encounter problems you can't imagine. Use fsocket to implement the get of the http protocol. Now most All websites use anti-collection technology and use information extraction technology when collecting content.
2. Analyze the code to get what you want (use regular matching here).
3. Carry out downloading and warehousing operations based on the obtained content.
4. Display data according to your web page layout
3. Share your personal collection experience:
1. Do not collect sites that are protected against hotlinking. In fact, you can fake the origin, but such sites collect The cost is too high
2. For sites that collect as quickly as possible, it is best to collect locally
3. When collecting, there are many times when you can store part of the data in the database first, and then proceed to the next step of processing.
4. You must handle errors when collecting. I usually skip it if the collection fails three times.
In the past, I would often get stuck picking out a piece of content just because I couldn’t pick it up.
5. Before entering the database, you must make a good judgment, check the legality of the content, and filter unnecessary strings.
4. Share a link address: Regular expression online test
http://tool.chinaz.com/regex
5. Share some commonly used PHP regular expressions
Regular expressions that match Chinese characters : [\u4e00-\u9fa5]
Matching double-byte characters (including Chinese characters): [^\x00-\xff]
Regular expression matching HTML tags: /|/
Regular expression matching IP address /(\d+)\.(\d+)\.(\d+)\. (\d+)/g
Regular expression matching email addresses: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.] \w+)*
Regular expression matching URL: http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]* )?
Image link in matching information: /(s|S)(r|R)(c|C)*=*('|")?(w||/|.)+('|" | *|>)?/
Use regular expressions to extract file names from URL addresses: /(.*\/)([^\.]+).*/ig
China phone number verification/ ((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*/
China Postal Code Verification/d{6}/
Email verification/w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/
ID card verification/d{18}|d{15 }/
Commonly used number verification /d{n}/ n is the specified length /d{n,m}/ The length range from n to m
Illegal character verification
Match illegal characters such as: & / ' |
Regular expression [^&/|'\]+
Date verification
The matching form is: 20030718,030718
Range: 1900--2099
Regular expression (( ((19){1}|(20){1})d{2})|d{2})[01]{1}d{1}[0-3]{1}d{1}
Related recommendations:
Example of PHP regular method to determine whether a string contains Chinese characters
PHP regular expression Summary of the formula
php regular expression processing method
The above is the detailed content of Detailed explanation of PHP regularity and data collection. For more information, please follow other related articles on the PHP Chinese website!

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

HTTPS significantly improves the security of sessions by encrypting data transmission, preventing man-in-the-middle attacks and providing authentication. 1) Encrypted data transmission: HTTPS uses SSL/TLS protocol to encrypt data to ensure that the data is not stolen or tampered during transmission. 2) Prevent man-in-the-middle attacks: Through the SSL/TLS handshake process, the client verifies the server certificate to ensure the connection legitimacy. 3) Provide authentication: HTTPS ensures that the connection is a legitimate server and protects data integrity and confidentiality.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools