0
0
PHPprogramming~5 mins

Preg_match_all for global matching in PHP - Cheat Sheet & Quick Revision

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

preg_match_all finds all matches of a pattern in a string, not just the first one. It returns the number of matches and stores all matches as an array in a variable passed by reference.

Click to reveal answer
beginner
What is the difference between preg_match and preg_match_all?

preg_match finds only the first match of a pattern in a string. preg_match_all finds all matches throughout the string.

Click to reveal answer
beginner
How do you access all matches found by preg_match_all?

Matches are stored in an array passed by reference as the third argument. You can loop through this array to use each match.

Click to reveal answer
beginner
What does the return value of preg_match_all represent?

The return value is the number of full pattern matches found in the string. If no matches, it returns 0.

Click to reveal answer
beginner
Example: What will preg_match_all('/\d+/', 'I have 2 apples and 10 oranges.', $matches); find?

It will find all numbers: 2 and 10. These will be stored in $matches[0] as ['2', '10'].

Click to reveal answer
What does preg_match_all return if no matches are found?
A0
Bfalse
Cnull
D1
Which argument of preg_match_all holds the matches?
ASecond argument
BThird argument
CFirst argument
DFourth argument
If you want to find only the first match, which function should you use?
Apreg_split
Bpreg_match_all
Cpreg_replace
Dpreg_match
What type of data does preg_match_all return in the matches array?
AString
BInteger
CArray of strings
DBoolean
Which pattern will match all words in a string using preg_match_all?
A/\w+/
B/\d+/
C/\s+/
D/[aeiou]/
Explain how preg_match_all works and how you use it to find all matches in a string.
Think about how you get all results, not just one.
You got /5 concepts.
    Describe the difference between preg_match and preg_match_all with an example.
    Focus on how many matches each function returns.
    You got /3 concepts.