Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php matches only numbers and letters

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how php matches only numbers and letters". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how php matches only numbers and letters".

In PHP, you can use regular expressions and the "preg_match ()" function to match numbers and letters, which is used to perform a matching of regular expressions with the syntax of "preg_match (" / ^ [a-zA-Z0-9] + $/ u ", which requires a matching string).

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.

Why does php only match numbers and letters?

The preg_match function is used to perform a regular expression match.

Grammar

Int preg_match (string $pattern, string $subject [, array & $matches [, int $flags = 0 [, int $offset = 0])

Search for a match between subject and the regular expression given by pattern.

Parameter description:

$pattern: the pattern to search for, in string form.

$subject: enter a string.

$matches: if the parameter matches is provided, it will be populated as the search results. $matches [0] will contain the text to which the full pattern matches, $matches [1] will contain the text matched by the first capture subgroup, and so on.

Flags:flags can be set to the following tag value: PREG_OFFSET_CAPTURE: if this tag is passed, a string offset (relative to the target string) is appended for each occurrence of the match. Note: this changes the array populated to the matches parameter so that each element becomes a string to which the 0th element is matched, and the first element is the offset of the matching string in the target string subject.

Offset: typically, the search starts at the beginning of the target string. The optional parameter offset is used to specify that the search starts from an unknown part of the target string (in bytes).

Return value

Returns the number of matches for pattern. Its value will be 0 (mismatch) or 1, because preg_match () will stop searching after the first match. Unlike preg_match_all (), it searches for subject until it reaches the end. If an error occurs, preg_match () returns FALSE.

Regular expression: / ^ [a-zA-Z0-9] + $/ u

Interpretation:

(1) "/": the definition of regular expression, fixed writing form

(2) "^": indicates the beginning

(3) "[]": represents a character group that matches any characters it contains. For example, "[ab]" matches "a" in "plain"

(4) "amurz": the range of letters that match the lowercase letter amerz

(5) "Amurz": indicates the range of letters that match the capital letter Amurz.

(6) "0-9": a number with a matching range of 0-9

(7) "+": indicates that the number of matches is one or more

(8) "$": matches the end of the input line. If the Multiline property of the RegExp object is set, $also matches the position before "\ n" or "\ r"

(9) "u": the last u is the pattern modifier, which, strictly speaking, can be a predefined constant. Indicates that unicode is used for matching.

Extended data:

The meaning of other regular expression symbols:

(1) "*": matches the previous subexpression any number of times. For example, zo* matches "z" as well as "zo" and "zoo". * equivalent to {0,}

(2) "? Matches the previous subexpression zero or once For example, "es?" Matches "do" or "does".? Equivalent to {0pl 1}

(3) "{n}": n is a nonnegative integer. The match was determined n times. For example, "o {2}" cannot match "o" in "Bob", but can match two o in "food"

(4) "{n,}": n is a non-negative integer. Match at least n times. For example, "o {2,}" does not match "o" in "Bob", but can match all o in "foooood". "o {1,}" is equivalent to "o +". "o {0,}" is equivalent to "o *"

(5) "{n ~ m}": M and n are non-negative integers, where n

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report