Bird
Raised Fist0
PHPprogramming~10 mins

Why regex is needed in PHP - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the string contains the word 'apple'.

PHP
<?php
$string = "I like apples.";
if (preg_match([1], $string)) {
    echo "Found apple!";
}
?>
Drag options to blanks, or click blank then click option'
A"^apple$"
B"apple"
C"/apple/i"
D"/apple/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word without slashes as pattern causes errors.
Forgetting to use regex delimiters.
2fill in blank
medium

Complete the code to replace all digits in the string with '#'.

PHP
<?php
$text = "My phone is 123-456-7890.";
$result = preg_replace([1], '#', $text);
echo $result;
?>
Drag options to blanks, or click blank then click option'
A"/\d/"
B"/\w/"
C"/\s/"
D"/\D/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using \w matches letters and digits, not just digits.
Using \s matches spaces, not digits.
3fill in blank
hard

Fix the error in the regex pattern to match an email address.

PHP
<?php
$email = "user@example.com";
if (preg_match([1], $email)) {
    echo "Valid email.";
} else {
    echo "Invalid email.";
}
?>
Drag options to blanks, or click blank then click option'
A"^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$"
B"/^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$/"
C"/^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$/i"
D"/^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}/"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting delimiters causes syntax errors.
Leaving out anchors may cause partial matches.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths for words longer than 3 characters.

PHP
<?php
$words = ['cat', 'elephant', 'dog', 'horse'];
$lengths = array_filter(array_map(function($word) {
    return strlen($word);
}, $words), function($word) {
    return $word [1] 3;
});
print_r($lengths);
?>
Drag options to blanks, or click blank then click option'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or == will not filter correctly.
Using != will include unwanted words.
5fill in blank
hard

Fill all three blanks to create an associative array of uppercase words and their lengths for words longer than 4 characters.

PHP
<?php
$words = ['apple', 'bat', 'carrot', 'dog'];
$result = array_filter(array_combine(array_map(function($word) {
    return [1];
}, $words), array_map(function($word) {
    return [2];
}, $words)), function($length) {
    return $length [3] 4;
});
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Astrtoupper($word)
Bstrlen($word)
C>
Dstrtolower($word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using strtolower instead of strtoupper.
Using wrong comparison operators like < or ==.

Practice

(1/5)
1. Why do we need regex (regular expressions) in PHP?
easy
A. To handle file uploads
B. To create graphical user interfaces
C. To find and work with specific text patterns easily
D. To connect to databases

Solution

  1. Step 1: Understand regex purpose

    Regex is designed to find patterns in text, like searching or validating strings.
  2. Step 2: Match purpose with PHP tasks

    PHP uses regex to quickly check or change text based on patterns, not for GUIs or databases.
  3. Final Answer:

    To find and work with specific text patterns easily -> Option C
  4. Quick Check:

    Regex = text pattern matching [OK]
Hint: Regex is for text pattern tasks, not UI or databases [OK]
Common Mistakes:
  • Confusing regex with database functions
  • Thinking regex creates user interfaces
  • Assuming regex handles file uploads
2. Which PHP function uses regex to check if a pattern exists in a string?
easy
A. preg_match()
B. str_replace()
C. array_search()
D. file_get_contents()

Solution

  1. Step 1: Identify regex functions in PHP

    preg_match() is the PHP function that checks if a regex pattern matches a string.
  2. Step 2: Compare with other functions

    str_replace() replaces text, array_search() searches arrays, file_get_contents() reads files, none use regex for matching.
  3. Final Answer:

    preg_match() -> Option A
  4. Quick Check:

    Regex match function = preg_match() [OK]
Hint: preg_ prefix means regex functions in PHP [OK]
Common Mistakes:
  • Choosing str_replace() which is not regex-based
  • Confusing array_search() with regex matching
  • Picking file_get_contents() which reads files
3. What will be the output of this PHP code?
$text = 'apple123';
if (preg_match('/\d+/', $text)) {
echo 'Number found';
} else {
echo 'No number';
}
medium
A. Number found
B. No number
C. Syntax error
D. Empty output

Solution

  1. Step 1: Understand the regex pattern

    The pattern '/\d+/' looks for one or more digits anywhere in the string.
  2. Step 2: Check the string content

    The string 'apple123' contains digits '123', so preg_match returns true and 'Number found' is printed.
  3. Final Answer:

    Number found -> Option A
  4. Quick Check:

    Digits in string = 'Number found' [OK]
Hint: \d+ means digits; 'apple123' has digits [OK]
Common Mistakes:
  • Thinking preg_match returns false if digits exist
  • Confusing regex pattern syntax
  • Assuming code has syntax errors
4. Find the error in this PHP regex code:
$email = 'user@example.com';
if (preg_match('^[\w.-]+@[\w.-]+\.[a-z]{2,4}$', $email)) {
echo 'Valid email';
} else {
echo 'Invalid email';
}
medium
A. preg_match() cannot validate emails
B. Pattern does not check for '@' symbol
C. Backslashes need double escaping in PHP strings
D. Regex delimiters missing

Solution

  1. Step 1: Check regex delimiters

    PHP preg functions require the pattern to start and end with delimiters like '/'.
  2. Step 2: Identify missing delimiters

    The pattern ^[\w.-]+@[\w.-]+\.[a-z]{2,4}$ lacks opening '/' and closing '/' delimiters, causing a 'No ending delimiter' error.
  3. Final Answer:

    Regex delimiters missing -> Option D
  4. Quick Check:

    preg_match needs /pattern/ delimiters [OK]
Hint: PHP regex patterns need delimiters like /pattern/ [OK]
Common Mistakes:
  • Forgetting regex delimiters
  • Thinking '@' is not checked
  • Believing preg_match can't validate emails
5. You want to extract all words starting with 'a' from a text using PHP regex. Which pattern should you use?
hard
A. '/a\b\w*/'
B. '/\ba\w*/i'
C. '/\w*a\w*/'
D. '/\b\w*a\b/'

Solution

  1. Step 1: Understand word boundary and pattern

    \b means word boundary, 'a' is the first letter, \w* matches the rest of the word, 'i' makes it case-insensitive.
  2. Step 2: Analyze options

    '/\ba\w*/i' correctly matches words starting with 'a'. Others either place 'a' incorrectly or match words containing 'a' anywhere.
  3. Final Answer:

    '/\ba\w*/i' -> Option B
  4. Quick Check:

    Words starting with 'a' = /\ba\w*/i [OK]
Hint: Use \b for word start, 'a' next, then \w* for rest [OK]
Common Mistakes:
  • Placing 'a' after word boundary incorrectly
  • Matching words containing 'a' anywhere
  • Forgetting case-insensitive flag
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard