PHP Level 2, Regular Expressions

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Johnkawakami
J
Johnkawakami
Community Contributor
Quizzes Created: 3 | Total Attempts: 3,333
Questions: 6 | Attempts: 2,137

SettingsSettingsSettings
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.


Questions and Answers
  • 1. 

    In the following statement, what does the "+" symbol mean?preg_match('/^[0-9]+$/', $data)

    • A.

      Any character

    • B.

      One character between 0 and 9

    • C.

      One or more characters between 0 and 9

    • D.

      Zero or more characters between 0 and 9

    • E.

      Any character between 0 and 9 followed by a $

    Correct Answer
    C. One or more characters between 0 and 9
    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.

    Rate this question:

  • 2. 

    What would match the regex in the following statement:preg_match('/^[0-9]{3}-[0-9]{2,4}$/', $data)

    • A.

      98090

    • B.

      213-99

    • C.

      415

    • D.

      1-2345

    • E.

      5-99

    Correct Answer
    B. 213-99
    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".

    Rate this question:

  • 3. 

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

    • A.

      Preg_match, strpos, ereg

    • B.

      Ereg, preg_match, strpos

    • C.

      Ereg, strpos, preg_match

    • D.

      Preg_match, ereg, strpos

    Correct Answer
    B. Ereg, preg_match, strpos
    Explanation
    strpos is fastest.

    Rate this question:

  • 4. 

    How long is the shortest string that would match this regex:preg_match('/^[a-z]+[0-9]*[A-Z]{2,10}$/')

    • A.

      0 characters (empty string)

    • B.

      1 character

    • C.

      2 characters

    • D.

      3 characters

    • E.

      4 characters

    Correct Answer
    D. 3 characters
    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.

    Rate this question:

  • 5. 

    What is the value of $matches[1]?preg_match('/([0-9]{3})-([0-9]{3})-([0-9]{4})/', "TEL 323-555-1212", $matches);

    • A.

      TEL

    • B.

      323

    • C.

      555

    • D.

      1212

    Correct Answer
    B. 323
    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.

    Rate this question:

  • 6. 

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

    • A.

      Validating input data

    • B.

      Dividing input text into substrings.

    • C.

      Converting hexadecimal into decimal.

    • D.

      Replacing strings with other strings.

    Correct Answer
    C. Converting hexadecimal into decimal.
    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.

    Rate this question:

Quiz Review Timeline +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.