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
More of Php Regex
- How to use PHP regex to match a zip code?
- How to use PHP regex to match a year?
- How to use PHP regex to match URL path?
- How to use PHP regex to match time?
- How to remove all non-numeric characters using PHP regex?
- How to remove non-printable characters using PHP regex?
- How to use PHP regex to match a line break?
- How to use PHP regex in a case insensitive mode?
- How to use PHP regex to match a hashtag?
- How to use PHP regex with zero or more occurrences?
See more codes...