With preg_match_all I want to get class and data-attributes in html.
The example below works, but it only returns class names or only data-id content.
I want the example pattern to find both class and data-id content.
Which regex pattern should I use?
Html contents:
<!-- I want to: $matches[1] == test_class | $matches[2] == null -->
<div class="test_class">
<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div class="test_class" data-id="1">
<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div class="id="test_id" class="test_class" data-id="1">
<!-- I want to: $matches[1] == test_class test_class2 | $matches[2] == 1 -->
<div class="test_class test_class2" id="test_id" data-id="1">
<!-- I want to: $matches[1] == 1 | $matches[2] == test_class test_class2 -->
<div data-id="1" class="test_class test_class2" id="test_id" >
<!-- I want to: $matches[1] == 1 | $matches[2] == test_class test_class2 -->
<div id="test_id" data-id="1" class="test_class test_class2">
<!-- I want to: $matches[1] == test_class | $matches[2] == 1 -->
<div class="test_class" id="test_id" data-id="1">
The regex that does not work as I want:
$pattern = '/<(div|i)\s.*(class|data-id)="([^"]+)"[^>]*>/i';
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire