Answer the question
In order to leave comments, you need to log in
How to use preg_match_all correctly?
It is required to get the tag in which the occurrence was found.
There is this PHP:
<?php
$string = '<body>
<h1>Текст!</h1>
<p><a href="#">текст ссылки</a></p>
<p><a href="#">текстище ссылки</a></p>
<table>
<tr><td>Текст 1</td></tr>
<tr><td>Текст 2</td></tr>
</table>
</body>';
if(preg_match_all("'<*?[\/\!]*?[^<>]*?>текст'i", $string, $result)) {
print_r($result);
}
?>
Answer the question
In order to leave comments, you need to log in
Do not use regular expressions to parse semantic data. HTML must be parsed as a subset of XML. There are several libraries, standard and additional, that fix invalid HTML and make convenient queries on the data
. Several links on this topic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question