⊗ppPsRgTNP 18 of 84 menu

The Problem of Tag Names When Parsing Attributes with Regular Expressions in PHP

The attribute src can be not only on images, but also on other tags:

<img src="1.png"> <script src="file.js"></script>

Also, there can be other attributes in the tag:

<img src="1.png"> <img class="image" src="1.png"> <script src="file.js"></script>

To avoid catching extra tags, we must specify the tag name in the regex. At the same time, we will also have to account for the presence of other attributes. Let's do this:

<?php preg_match_all('#<img[^>]+src\s*=\s*(["\'])(.+?)\1#su', $str, $matches, PREG_PATTERN_ORDER); ?>

Let's check that everything works:

<?php var_dump($matches[2]); ?>

Find the contents of the href attributes of all links:

<a href="page1.html">link1</a> <a href='page2.html'>link2</a> <a class="link" href = "page3.html">link3</a> <link rel="stylesheet" href="styles.css">
enruptmsit