Home > Backend Development > PHP Problem > How to match only numbers and letters in php

How to match only numbers and letters in php

WBOY
Release: 2023-03-15 13:48:02
Original
3797 people have browsed it

In PHP, you can use regular expressions and the "preg_match()" function to match numbers and letters. This function is used to perform a regular expression matching. The syntax is "preg_match("/^[a -zA-Z0-9] $/u", the string to be matched)".

How to match only numbers and letters in php

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

How to match only numbers and letters in php

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

Syntax

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

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

Parameter description:

  • $pattern: The pattern to be searched, in string form.

  • $subject: Input string.

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

  • $flags: flags can be set to the following flag values: PREG_OFFSET_CAPTURE: If this flag is passed, the string offset (relative to target string). Note: This will change the array filled in the matches parameter so that each element becomes a string where the 0th element is the matched string and the 1st element is the offset of the matched string in the target string subject. .

  • offset: Normally, the search starts from the beginning of the target string. The optional parameter offset is used to specify the search starting from an unknown point in the target string (unit is bytes).

Return value

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

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

Interpretation:

(1) "/" : Definition of regular expression, fixed writing form;

(2) "^": indicates the beginning;

(3) "[]": indicates a character group, matching the included any character. For example, "[ab]" matches "a" in "plain";

(4) "a-z": a range of letters matching lowercase letters a-z;

(5) "A-Z ": Indicates the range of letters matching the uppercase letters A-Z;

(6) "0-9": Matches numbers in the range 0-9;

(7) " ": Indicates matching The number 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 mode modifier, strictly speaking , which can be a predefined constant. Indicates using unicode for matching.

Extended information:

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, "do(es)?" matches "do" or "does". ? Equivalent to {0,1};

(3) "{n}": n is a non-negative integer. Matches are determined n times. For example, "o{2}" cannot match the "o" in "Bob", but can match the two o's in "food";

(4) "{n,}": n is a non-negative integer. Match at least n times. For example, "o{2,}" cannot match the "o" in "Bob", but it can match all o's 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

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to match only numbers and letters in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template