简体   繁体   中英

Simple-html-dom skips attributes

I am trying to parse html page of Google play and getting some information about apps. Simple-html-dom works perfect, but if page contains code without spaces, it completely ingnores attributes. For instance, I have html code:

<div class="doc-banner-icon"><img itemprop="image"src="https://lh5.ggpht.com/iRd4LyD13y5hdAkpGRSb0PWwFrfU8qfswGNY2wWYw9z9hcyYfhU9uVbmhJ1uqU7vbfw=w124"/></div>

As you can see, there is no any spaces between image and src , so simple-html-dom ignores src attribute and returns only <img itemprop="image"> . If I add space, it works perfectly. To get this attribute I use the following code:

foreach($html->find('div.doc-banner-icon') as $e){          
        foreach($e->find('img') as $i){
            $bannerIcon = $i->src;              
        }
}

My question is how to change this beautiful library to get full inner text of this div ?

I just create function which adds neccessary spaces to content:

function placeNeccessarySpaces($contents){
$quotes = 0; $flag=false;
$newContents = '';
for($i=0; $i<strlen($contents); $i++){
    $newContents.=$contents[$i];
    if($contents[$i]=='"') $quotes++; 
    if($quotes%2==0){
        if($contents[$i+1]!== ' ' && $flag==true) {             
            $newContents.=' ';
            $flag=false;
        }           
    }
    else $flag=true;        
}   
return $newContents;
}

And then use it after file_get_contents function. So:

$contents = file_get_contents($url, $use_include_path, $context, $offset);
$contents = placeNeccessarySpaces($contents);

Hope it helps to someone else.

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