M
M
Maxim E2016-03-04 12:45:55
PHP
Maxim E, 2016-03-04 12:45:55

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);
}
?>

It is required that case-insensitive matches are taken into account and only the whole word (so that "text" does not fall under the condition).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Volintsev, 2016-03-05
@copist

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.

  • https://code.google.com/archive/p/phpquery/
  • php.net/manual/ru/domdocument.loadhtml.php
  • parsing-and-i.blogspot.ru/2009/08/html-php.html
  • https://github.com/hxseven/htmlSQL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question