简体   繁体   中英

Find only first a elements in li (excluding ul elements in li) using simplehtmldom

I am using simplehtmldom to find specific elements in web-site.

My code

 function strpos_arr($haystack) {
    $needle  = array('menu', 'nav');
    if(!is_array($needle)) $needle = array($needle);
    foreach($needle as $what) {
        if(($pos = strpos($haystack, $what))!==false)
            return true;
    }
    return false;
}

$first = true;
foreach($html->find('ul') as $ul){
    if ( strpos_arr($ul->id) OR strpos_arr($ul->class)  ) {
        if ( $first )
        {
            foreach($ul->find('li a') as $li)
            {
                echo $li.'<br>';
            }
            $first = false;
        }
    }
}
?> 

This code shows all links in all li elements including links that are nested inside ul elements under li elements. I need to echo out only the main a elements in li not those nested under ul subs.

EDIT:

The lines needed are marked as "a href I need"

<ul id="parent">
    <li>a href I need<li>
    <li>a href I need<li>
    <li>a href I need<li>
    <li>a href I need<li>
        <ul id="sub">
            <li>a href I DON'T need<li>
            <li>a href I DON'T need<li>
        </ul>
    <li>a href I need<li>
    <li>a href I need<li>
</ul>

I tried to use CSS selectors to filter elements, but it didn't work... So I used DOM functions to make sure that the actual element's parent is parent

Here's a working code that gives you the requested li nodes:

$text = '
        <ul id="parent">
            <li>a href I need</li>
            <li>a href I need</li>
            <li>a href I need</li>
            <li>a href I need</li>
                <ul id="sub">
                    <li>a href I DON\'T need</li>
                    <li>a href I DON\'T need</li>
                </ul>
            <li>a href I need</li>
            <li>a href I need</li>
        </ul>';

//Create a DOM object
$html = new simple_html_dom();

// Load HTML from a string
$html->load($text);

// Find li elmenets within ul tags
$list = $html->find('ul+li');

// Find succeeded
if ($list) {
    echo "<br/> Found ". count($list);

    // Display output as code
    echo "<pre>";

    foreach ($list as $key => $elm) {
        if($elm->parent()->id == "parent") {
            echo htmlentities($elm->outertext);
            echo "<hr/>";
        }
    }

    echo "</pre>";
}
else
    echo "Find function failed !";

PHP Fiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM