B
B
Bestolkovy2017-02-13 06:31:25
PHP
Bestolkovy, 2017-02-13 06:31:25

How to use XPath to find the contents of a container with a class?

Good afternoon!
It is necessary to find all containers with the `auto-formatting` class in the body of the body tag and process them.
I'm looking like this:

preg_match( '/\<div class\=\"auto-formatting\"\>([\s\S]*?)\<\/div\>\<\!-- \.auto-formatting --\>/miu', func_get_arg(0), $matches );

But this approach is not at all flexible.
How can this be done with XPath?
So far I've only thought of this:
$i = 0;
$html = DOMDocument::loadHTML( $buffer );
$xpath = new DOMXPath($htmll);

    foreach( $xpath->query('//div[contains(@class,"auto-formatting")]') as $item ) {
        // обработка
        $i++;
    }

Does not work. Tell me, please, how to write correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sundukov, 2017-02-13
@Bestolkovy

The expression is correct. There is a typo in the code. The DOM is loaded into the $html variable, and the XPath is applied to the document from the $html l variable . In general, if $htmll was not initialized, then it should have failed with an error. In general, it is worth turning on the display of errors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question