9951 explained code solutions for 126 technologies


php-regexHow to match an HTML tag using regex in PHP?


Matching HTML tags using regex in PHP can be done using the preg_match() function. This function takes two parameters, the first being the regular expression pattern and the second being the string to match against.

$html = '<p>This is a paragraph</p>';

if (preg_match('/<p>(.*?)<\/p>/', $html, $matches)) {
    echo $matches[1];
}

The output of the above code will be:

This is a paragraph

Code explanation

  • preg_match(): This is the PHP function used to match a regular expression pattern against a string.
  • /<p>(.*?)<\/p>/: This is the regular expression pattern used to match an HTML paragraph tag. The <p> and </p> are the opening and closing tags, and the .*? is a wildcard that matches any character.
  • $matches: This is an array that will contain the matches found by the regular expression.
  • echo $matches[1]: This will output the contents of the first match found by the regular expression.

Helpful links

Edit this code on GitHub