PHP Level 2, Regular Expressions

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Johnkawakami
J
Johnkawakami
Community Contributor
Quizzes Created: 3 | Total Attempts: 3,430
| Attempts: 2,198 | Questions: 6
Please wait...
Question 1 / 6
0 %
0/100
Score 0/100
1. What would match the regex in the following statement:

preg_match('/^[0-9]{3}-[0-9]{2,4}$/', $data)

Explanation

The given regular expression '/^[0-9]{3}-[0-9]{2,4}$/' matches strings that have the following pattern: three digits followed by a hyphen, followed by two to four digits. Therefore, the only string that matches this pattern is "213-99".

Submit
Please wait...
About This Quiz
PHP Level 2, Regular Expressions - Quiz

This quiz is a test of basic PCRE, aka, preg_match(). Difficult concepts in regexes are not covered. Passing the test indicates that you have a basic understanding of regexes.

Tell us your name to personalize your report, certificate & get on the leaderboard!
2. In the following statement, what does the "+" symbol mean?

preg_match('/^[0-9]+$/', $data)

Explanation

The "+" symbol in the given statement means that there should be one or more characters between 0 and 9. This means that the regular expression will match any string that consists of one or more digits from 0 to 9.

Submit
3. Which of the following is NOT a typical use for regular expressions in PHP.

Explanation

Regular expressions in PHP are commonly used for validating input data, dividing input text into substrings, and replacing strings with other strings. However, converting hexadecimal into decimal is not a typical use for regular expressions in PHP. This task can be accomplished using built-in PHP functions or algorithms specifically designed for conversion between number systems.

Submit
4. How long is the shortest string that would match this regex:

preg_match('/^[a-z]+[0-9]*[A-Z]{2,10}$/')

Explanation

The given regular expression requires a string that starts with one or more lowercase letters ([a-z]+), followed by zero or more digits ([0-9]*), and ends with 2 to 10 uppercase letters ([A-Z]{2,10}). In order to match this pattern, the string needs to have at least one lowercase letter, zero digits, and two uppercase letters. Therefore, the shortest string that would match this regex is "aAA", which has 3 characters.

Submit
5. What is the value of $matches[1]?

preg_match('/([0-9]{3})-([0-9]{3})-([0-9]{4})/', "TEL 323-555-1212", $matches);

Explanation

The value of $matches[1] is 323. This is because the preg_match function is used to search for a pattern in a string. In this case, the pattern is defined as '/([0-9]{3})-([0-9]{3})-([0-9]{4})/'. The string "TEL 323-555-1212" is searched for this pattern. The pattern consists of three groups, each enclosed in parentheses. The first group ([0-9]{3}) matches three digits, the second group matches another three digits, and the third group matches four digits. The matched values are stored in the $matches array, with $matches[1] representing the first group. Therefore, the value of $matches[1] is 323.

Submit
6. Rank these three function in the order of their typical speed, slowest to fastest:
preg_match(), strpos(), ereg().

Explanation

strpos is fastest.

Submit
View My Results

Quiz Review Timeline (Updated): Aug 18, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Aug 18, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 15, 2009
    Quiz Created by
    Johnkawakami
Cancel
  • All
    All (6)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What would match the regex in the following...
In the following statement, what does the "+" symbol...
Which of the following is NOT a typical use for regular expressions in...
How long is the shortest string that would match this...
What is the value of...
Rank these three function in the order of their typical speed, slowest...
Alert!

Advertisement