PHP Classes

File: example/example_find_text.php

Recommend this page to a friend!
  Classes of Lars Moelleken   Simple HTML DOM   example/example_find_text.php   Download  
File: example/example_find_text.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple HTML DOM
Manipulate HTML elements using DOMDocument
Author: By
Last change: Update of example/example_find_text.php
Date: 1 year ago
Size: 1,095 bytes
 

Contents

Class file image Download
<?php

use voku\helper\SimpleHtmlDomInterface;
use
voku\helper\SimpleHtmlDomNode;
use
voku\helper\SimpleHtmlDomNodeInterface;

require_once
'../vendor/autoload.php';

/**
 * @param \voku\helper\HtmlDomParser $dom
 * @param string $selector
 * @param string $keyword
 *
 * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
 */
function find_contains(
    \
voku\helper\HtmlDomParser $dom,
   
string $selector,
   
string $keyword
) {
   
// init
   
$elements = new SimpleHtmlDomNode();

    foreach (
$dom->find($selector) as $e) {
        if (
strpos($e->innerText(), $keyword) !== false) {
           
$elements[] = $e;
        }
    }

    return
$elements;
}

// -----------------------------------------------------------------------------

$html = '
<p class="lall">lall<br></p>
<p class="lall">foo</p>
<ul><li class="lall">test321<br>foo</li><!----></ul>
'
;

$document = new \voku\helper\HtmlDomParser($html);

foreach (
find_contains($document, '.lall', 'foo') as $child_dom) {
    echo
$child_dom->html() . "\n";
}