php-regexHow to use PHP regex to match an XML tag?
PHP regex can be used to match an XML tag. The following example code block shows how to use regex to match an XML tag:
$string = '<tag>content</tag>';
$pattern = '/<(.+?)>(.*?)<\/(.+?)>/';
preg_match($pattern, $string, $matches);
The output of the example code is:
Array
(
[0] => <tag>content</tag>
[1] => tag
[2] => content
[3] => tag
)
Code explanation
$string
: the string to be matched$pattern
: the regex pattern used to match the stringpreg_match()
: the PHP function used to match the string with the regex pattern
Helpful links
More of Php Regex
- How to use quantifiers in PHP regex?
- How to use PCRE in PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match an exact string?
- How to get the first match when using regex in PHP?
- How to use PHP regex with zero or more occurrences?
- How to use an "or" condition in PHP regex?
- How to match the end of a string when using regex in PHP?
- How to use PHP regex to match a zip code?
- How to match a space using PHP regex?
See more codes...