0
0
PHPprogramming~5 mins

Preg_match for pattern matching in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the preg_match function do in PHP?

preg_match checks if a string matches a specific pattern defined by a regular expression. It returns 1 if a match is found, 0 if not, and false on error.

Click to reveal answer
beginner
How do you write a basic preg_match call to check if the string "hello123" contains digits?
preg_match('/\d+/', 'hello123');

This checks if there is one or more digits anywhere in the string.

Click to reveal answer
beginner
What is the purpose of the delimiters (like /) in a preg_match pattern?

Delimiters mark the start and end of the regular expression pattern. They tell PHP where the pattern begins and ends.

Click to reveal answer
intermediate
How can you capture the matched part of a string using preg_match?

Use a third argument as an array to store matches. For example:

preg_match('/(\d+)/', 'abc123', $matches);
echo $matches[1]; // outputs '123'
Click to reveal answer
beginner
What does preg_match return if the pattern is not found in the string?

It returns 0, which means no match was found.

Click to reveal answer
What does preg_match('/abc/', 'abcdef') return?
Afalse
B0
C1
Dnull
Which symbol is commonly used as a delimiter in preg_match patterns?
AAll of the above
B#
C/
D~
How do you store the matched substring from preg_match?
ABy passing a second argument as an array
BYou cannot store matches
CBy passing a fourth argument as a string
DBy passing a third argument as an array
What does the pattern /\d+/ match?
AAny special character
BOne or more digits
CAny whitespace
DOne or more letters
If preg_match returns false, what does it mean?
AAn error occurred
BNo match found
CMatch found
DPattern is empty
Explain how to use preg_match to check if a string contains a specific word and how to get the matched word.
Think about capturing groups and the third argument.
You got /4 concepts.
    Describe the return values of preg_match and what each means.
    There are three possible return values.
    You got /3 concepts.