The content of this article is about regular expressions in PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it
Regular expression is a grammatical rule that describes the result of a string. It is a specific formatting pattern that can match, replace, and intercept matching strings. Commonly used languages basically have regular expressions, such as JavaScript, java, etc. In fact, as long as you understand the regular use of one language, it is relatively simple to use the regular rules of other languages. Okay, let’s start writing regular rules.
Related recommendations:
1. Regular expression syntax tutorial (including online testing tools)
2. PHP regular expression quick introduction video tutorial
When regular expressions match strings, they follow the following two basic principles:
1. The leftmost principle: Regular expressions always start from the target string. Starting from the leftmost position, matching is performed sequentially until the part that meets the requirements of the expression is matched, or until the end of the target string is matched.
2. The longest principle: For the matched target string, the regular expression will always match the longest part that meets the requirements of the regular expression; that is, the greedy mode
So what? To start, first start with the delimiter, which is commonly used to include /; #;~, which is used to indicate the beginning of a series of regular expressions. For example: ‘/a.*a/’. When the expression has too many escape characters, it is recommended to use # first, such as url;
$str = 'http://baidu.com'; $pattern = '/http:\/\/.*com/';//需要转义/ preg_match($pattern,$str,$match); var_dump( $match);
$str = 'http://baidu.com'; $pattern = '#http://.*com#';//不需要转义/ preg_match($pattern,$str,$match); var_dump( $match);
Now that you know how to write the beginning and the end, the next step is to judge the middle. Regular expressions are spliced using atoms and metacharacters from left to right.
For example, 'zxcv', when matching, '/.*/', where .* represents zxcv.
So what are the common atoms and metacharacters?
• \d Matches a numeric character. Equivalent to [0-9].
• \D Matches a non-numeric character. Equivalent to [^0-9].
• \f matches a form feed character. Equivalent to \x0c and \cL.
• \n Matches a newline character. Equivalent to \x0a and \cJ.
• \rmatches a carriage return character. Equivalent to \x0d and \cM.
• \s Matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v].
• \S matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v].
• \tmatches a tab character. Equivalent to \x09 and \cI.
• \v Matches a vertical tab character. Equivalent to \x0b and \cK.
• \w Matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]'.
• \W matches any non-word character. Equivalent to ‘[^A-Za-z0-9_]’.
• \xn Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be exactly two digits long. For example, '\x41' matches "A". ‘\x041’ is equivalent to ‘\x04’ & “1”. ASCII encoding can be used in regular expressions.
• \nmIdentifies an octal escape value or a backreference. If \nm is preceded by at least nm get-subexpressions, nm is a backward reference. If \nm is preceded by at least n obtains, n is a backward reference followed by a literal m. If none of the previous conditions are met, and if n and m are both octal digits (0-7), • \nm will match the octal escape value nm.
• \nmlIf n is an octal number (0-3), and m and l are both octal numbers (0-7), then Matches the octal escape value nml.
• \unUnicode characters represented by hexadecimal numbers. For example, \u00A9 matches the copyright symbol (?).
• . Matches any single character except "\n"
• ^ Matches the beginning of the input string. In the character field [], it means negation, such as '[^\w]' equals '\w'; and ^\w means starting with a word character.
• $ Matches the end position of the input string. For example '\w$' means ending with a word character.
• ? Matches the preceding subexpression zero or once is equivalent to {0,1}, for example, "do(es)?" can match "do" or "does".
• * Matches the previous subexpression zero or more times , equivalent to {0,}. For example, zo* matches "z", "zo", 'zoo'.
• Matches the previous subexpression one or more times, equivalent to {1,}. For example, 'zo ' can match "zo" and "zoo".
• {n} n is a non-negative integer, matched n times. For example, 'o{2}' doesn't match "Bob" or 'Booob', but it does match the two 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 it matches all o's in "foooood". 'o{1,}' is equivalent to 'o '. 'o{0,}' is equivalent to 'o*'.
• {n,m} m and n are both non-negative integers, where n
• [] Character set (character field). Matches any one of the characters contained. For example, '[abc]' matches 'a' in "plain".
• () Match the content in () and get this match. With \n (n is an integer greater than 1), 'http://baidu.com' matches 'http://baidu' if the expression: '(\w) (:)\/\/.*\1' .comhttp',\1 means http.
• (?:) matches but does not obtain the matching result and does not store it for later use. This is useful when using the "or" character (|) to combine parts of a pattern. For example, 'industr(?:y|ies) is a shorter expression than 'industry|industries'. If the above expression is changed to '(?:\w )(:)\/\/.*\1', then \1 is expressed as:
• | x|y,匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。
• [-] 字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。
• (?=pattern)正 向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹 配不需要获取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ,但不能匹配 "Windows 3.1" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹 配的搜索,而不是从包含预查的字符之后开始。
• (?!pattern)负 向预查,在任何不匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不 需要获取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows",但不能匹配 "Windows 2000" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜 索,而不是从包含预查的字符之后开始
有时候最后定界符会有一个字母,如‘/as.*/i’,那这个i又是什么呢,这就是模式修正符;
i表示在和模式进行匹配进不区分大小写
m将模式视为多行,使用^和$表示任何一行都可以以正则表达式开始或结束
s如果没有使用这个模式修正符号,元字符中的"."默认不能表示换行符号,将字符串视为单行
x表示模式中的空白忽略不计
e正则表达式必须使用在preg_replace替换字符串的函数中时才可以使用(讲这个函数时再说)
A以模式字符串开头,相当于元字符^
Z以模式字符串结尾,相当于元字符$
U正则表达式的特点:就是比较“贪婪”,使用该模式修正符可以取消贪婪模式
例:
$str = 'asddadsdasd'; $pattern = '/a.*d/'; preg_match($pattern,$str,$match); var_dump($match) ;//asddadsdasd; $str = 'asddadsdasd'; $pattern = '/a.*d/U';//$pattern = '/a.*?d/'; preg_match($pattern,$str,$match); var_dump($match) ;//asd
php常用正则函数;
匹配:preg_match()与preg_match_all()
1 preg_match($pattern,$subject,[array &$matches])
2 preg_match_all($pattern,$subject,array &$matches)
1只会匹配一次,2会把所有符合的字符串都匹配出来,并且放置到matches数组中,而且这两个函数都有一个整形的返回 值。1是一维数组,2是二维数组
替换:preg_replace()
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
搜索subject中匹配pattern的部分, 以replacement进行替换。
相关推荐:
The above is the detailed content of php regular expression. For more information, please follow other related articles on the PHP Chinese website!

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.


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

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
