In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use preg_replace () to replace strings in PHP. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
The data that needs to be processed by the program is not always designed with database thinking in advance, or cannot be stored with the structure of the database.
For example, template engine parsing templates, spam sensitive information filtering and so on.
In general, we use regularities to match preg_match and replace preg_replace according to our rules.
But in general applications, they are nothing more than database CRUD, and there are few opportunities for regular fiddling.
As mentioned earlier, there are two scenarios: statistical analysis, with matching, and processing with substitution.
PHP preg_replace () regular substitution, unlike Javascript regular substitution, PHP preg_replace () replaces the elements of all symbol matching conditions by default.
The copy code is as follows:
Preg_replace (regular expression, replace with, string, maximum number of replacements [default-1, innumerable], number of substitutions)
Regular expressions in most languages are similar, but there are slight differences.
PHP regular expression
Regular character regular interpretation\ marks the next character as a special character, or a literal character, or a backward reference, or an octal escape character. For example, "\ n" matches the character "n". "\" matches a newline character. The sequence "\" matches "\" and "(" matches "(". ^ matches the starting position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after "\ n" or "\ r". $matches the end of the input string. If the Multiline property of the RegExp object is set, $also matches the position before "\ n" or "\ r". * matches the previous subexpression zero or more times. For example, zo* can match "z" and "zoo". * is equivalent to {0,}. + matches the previous subexpression one or more times. For example, "zo+" matches "zo" and "zoo", but not "z". + is equivalent to {1,}. ? Matches the previous subexpression zero or once. For example, "do (es)?" Can match "do" in "does" or "does". ? It is equivalent to {0jue 1}. {n} n is a non-negative integer. Match the determined n times. For example, "o {2}" does not match "o" in "Bob", but can match two o in "food". {n,} n is a non-negative integer. Match at least n times. For example, "o {2,}" does not match "o" in "Bob", but does match all o in "foooood". "o {1,}" is equivalent to "o +". "o {0,}" is equivalent to "o *". {n ·m} m} m and n are nonnegative integers, where nx | y matches x or y. For example, "z | food" can match "z" or "food". "(z | f) ood" matches "zood" or "food". [xyz] character collection. Matches any of the characters contained. For example, "[abc]" can match "a" in "plain". [^ xyz] negative character collection. Matches any characters that are not included. For example, "[^ abc]" can match "plin" in "plain". [amurz] character range. Matches any character within the specified range. For example, "[a murz]" can match any lowercase character in the range of "a" to "z". Note: the range of characters can be represented only when the hyphen is within the character group and between two characters; if the beginning of the character group is out, it can only represent the hyphen itself. [^ amerz] negative character range. Matches any character that is not within the specified range. For example, "[^ amurz]" can match any character that is not in the range of "a" to "z". \ b matches a word boundary, that is, the position between the word and the space. For example, "er\ b" can match "er" in "never", but not "er" in "verb". \ B matches non-word boundaries. "er\ B" matches "er" in "verb", but not "er" in "never". \ cx matches the control characters indicated by x. For example,\ cM matches a Control-M or carriage return. The value of x must be one of Amurz or aMuz. Otherwise, c is treated as a literal "c" character. \ d matches a numeric character. Equivalent to [0-9]. \ D matches a non-numeric character. Equivalent to [^ 0-9]. \ f matches a feed character. Equivalent to\ x0c and\ cL. \ nmatches a newline character. Equivalent to\ x0a and\ cJ. \ r matches a carriage return. Equivalent to\ x0d and\ cM. \ s matches any white space characters, including spaces, tabs, page breaks, and so on. Equivalent to [\ f\ n\ r\ t\ v]. \ s matches any non-white space character. Equivalent to [^\ f\ n\ r\ t\ v]. \ t matches a tab. Equivalent to\ x09 and\ cI. \ v matches a vertical tab. Equivalent to\ x0b and\ cK. \ w matches any word characters that include underscores. Equivalent to "[A-Za-z0-9]". \ W matches any non-word characters. Equivalent to "[^ A-Za-z0-9]". \ xn matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be a determined two-digit length. For example, "\ x41" matches "A". "\ x041" is equivalent to "\ x041". ASCII encoding can be used in regular expressions. \ num matches num, where num is a positive integer. A reference to the obtained match. For example, "(.)\ 1" matches two consecutive identical characters. \ nidentifies an octal escape value or a backward reference. If there are at least n previous acquired subexpressions, n is a backward reference. Otherwise, if n is an octal number (0-7), n is an octal escape value. \ nm identifies an octal escape value or a backward reference. If there are at least nm acquired subexpressions before\ nm, nm is a backward reference. If there are at least n fetches before\ nm, n is a backward reference followed by the text m. If none of the previous conditions are met, if both n and m are octal numbers (0-7),\ nm will match the octal escape value nm. \ nml if n is an octal number (0-7) and m and l are both octal numbers (0-7), the octal escape value nml is matched. \ un matches n, where n is a Unicode character represented by four hexadecimal digits. For example,\ u00A9 matches the copyright symbol (©).
The above table is a more comprehensive explanation of regular expressions, while the regular characters in the trademark have a special meaning and no longer represent the original character meaning. For example, "+" in a regular expression does not represent a plus sign, but represents one or more matches. If you want "+" to indicate a plus sign, you need to precede it with "\" escape, that is, using "\ +" to indicate a plus sign.
The copy code is as follows:
The regular expression of 1 / 1 / 2 is: 1\ + 1 / 2
And the regular expression 1-1-2 can represent multiple 1-2, that is:
11 regular expression: 1 regular expression 1 regular expression
111'2 regular expression: 1'1'2
Regular expression 11111x 2: 1x 1m 2
……
In other words, all regular characters have a specific meaning, and if you need to express the meaning of the original character, you need to add "\" escape in front of it. Even if non-regular characters, there is no problem with "\" escape.
The copy code is as follows:
1 "1" 2 regular expression can also be:\ 1\ +\ 1\ =\ 2
All characters are escaped, but this is not recommended.
Regular expressions must be surrounded by delimiters, which are "/" in Javascript, while in PHP, it is more common to delimit with "/" or "#", and the outside needs to be enclosed in quotation marks.
If the regular expression contains these delimiters, you need to escape these characters.
PHP regular expression delimiter
Regular expressions in most languages are delimited by "/", but in PHP, you can also use "#" to delimit. If the string contains a large number of "/" characters, you need to escape these "/" when using "/" delimiting, while using "#" does not need to escape, which is more concise.
The copy code is as follows:
Through the above two PHP regular replacement code, we can find that if the regular statement contains a large number of "/", it is OK to use "/" or "#" as the delimiter, but using "#" can make the code look more concise. But E-dimensional technology recommends that you keep using "/" as the delimiter, because in languages such as Javascript, you can only use "/" as the delimiter, which can form a habit of being written in other languages.
PHP regular expression modifier
The modifier is placed at the end of the PHP regular expression delimiter "/", before the quotation marks at the end of the regular expression.
The copy code is as follows:
I ignore case, match does not consider case
M multiple lines match independently, and if the string does not contain newline characters such as [\ n], it is the same as the normal regular.
S sets the regular symbol. You can match the newline character [\ n], if not set, the regular symbol. Cannot match newline character\ n.
X ignore spaces without escape
E eval () executes the function on the matched element.
A pre-anchoring, constraint matching only starts with the target string search
D locks $as the end, and if there is no D, if the string contains newline characters such as [\ n], $still matches the newline character. If the modifier m is set, the modifier D is ignored.
S analysis of unanchored matching
U is not greedy. If you add "?" after the regular character quantifier, you can restore greed.
X opens an perl incompatible attachment
U enforces the string to be UTF-8-encoded, which is generally required in non-UTF-8-encoded documents. It is not recommended to use this in the UTF-8 environment. According to the E-dimensional Technology Survey, using this will have a Bug. This Bug address:
If you are familiar with Javascript's regular expressions, you may be familiar with the Javascript regular expression modifier "g", which means to match all eligible elements. In PHP regular substitution, it is the element that matches all symbolic conditions, so there is no Javascript modifier "g".
PHP regular Chinese and ignore case PHP preg_replace () are case-sensitive and can only match strings in ASCII encoding. If you need to match characters such as case-insensitive and Chinese characters, you need to add the corresponding modifier I or u.
The copy code is as follows:
Both case and Chinese are sensitive in PHP, but in Javascript rules, only case is sensitive, and ignoring case is also done by modifier I, but Javascript does not need to tell whether it is a special character such as UTF-8 Chinese, it can match Chinese directly.
PHP regular newline instance
When a PHP regular expression encounters a newline, it treats the newline as a normal character in the middle of the string. And universal symbols. Cannot match\ n, so there are a lot of points when you encounter a string regular with a newline character.
The copy code is as follows:
In the future, when you use PHP to grab the content of a website and replace it with regular batch, you can't avoid ignoring that the acquired content contains newline characters, so be careful when using regular replacement.
PHP regular matching executes the function PHP regular substitution can use a modifier e, representing eval (), to execute a function after matching.
The copy code is as follows:
According to the above code, although the matched function strtolower () is in quotation marks, it will still be executed by eval ().
Regular replacement matching variables are referenced back
If you are familiar with Javascript, you must be sure about $1 $2 $3. Equal backward references are familiar, and these can also be used as backward reference parameters in PHP. In PHP, you can also use\ 1\\ 1 to indicate a backward reference.
The concept of backward reference is to match a large fragment, and the inside of the regular expression is cut into several small matching elements with parentheses, so that each matching element is replaced by a backward reference according to the parenthesis sequence.
The copy code is as follows:
After reading the above, do you have any further understanding of how to use preg_replace () to replace strings in PHP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.