简体   繁体   中英

PHP Simple HTML DOM Parser find elements with multiple attributes

I'm using Simple HTML DOM Parser but can't figure out how to get the following :

Imagine I have this :

<td style"color:#e05206" width"22" height="58">OK</td>
<td style"color:#e05206" width"22" height="58" align="center">NOT OK</td>
<td style"color:#ffffff" width"22" height="58">NOT OK</td>

I want to get OK.

I tried something like this :

$results = $html->find('td[style*=color:#e05206], td[width*="22"], td[height*=58], td[!align]');

I thought that it would be the right answer, but it is not, because it applies an OR and not an AND .

I want to get only OK and not all the elements. To summarize i would like the td that has V AND X AND Y AND NOT Z. Is it possible ?

Thanks for your answers !

Update : I am not searching to get OK, it could be anything else. I am searching to get a td that has an attribute V=something AND X=something AND Y=something AND NOT Z.

var OkElement = new Array();
$("table tr").find("td").each(function(){
   tdValue = $(this).text();
   if(tdValue == "OK"){
      OkElement.push($(this).attr('id');
   }
});

Explanation : Here We are looping all the td DOM Elements and then checking for OK Value. For Each loop this indicates current td elements and getting the td value and checking whether text value is OK or not if it is OK value then we pushing the td elemtnt ID into array. Finally array contains only td element Ids which contains OK text. Note : You should give unique id for each td element.

    <td id="x1" style"color:#e05206" width"22" height="58">OK</td>
    <td id="x2" style"color:#e05206" width"22" height="58" align="center">NOT OK</td>
    <td id="x3" style"color:#ffffff" width"22" height="58">NOT OK</td>

Because eventhough you are getting td element you cant manipulate on the corresponding td element without having id. Now you will get x1 in OkElement Array now you can manipulate that div as you wish Example

for(var i=0;i<OkElement.length;i++){
    $("#"+OkElement[i]).attr('style','background-color : red');
}

Here in my example i am applying background color red for td elements whose have OK

var OkElement = new Array();
$("table tr").find("td").each(function(){
   tdValue = $(this).text();
   if(tdValue == "OK"){
      OkElement.push($(this).css('color')
   }
});

Explanation : Here We are looping all the td DOM Elements and then checking for OK Value.
    for Each loop this indicates current td elements and getting the td value and checking whether text value is OK or not if it is OK value then we pushing the td elemtnt color style into array...
    Finally array contains only #e05206 which contains OK text. 





    Now you will get #e05206 in OkElement Array now you can manipulate that tds as you wish
    Example 

Ideally you want to be able to get at that one with:

'td:not([align])[style="color:#e05206"]'

or

'td[style="color:#e05206"]:not([align])'

You can't do that with simple html dom though because it is simple.

The good news is you can do it with phpquery.

Ok so this is what i have done.

I used the ganon lib.

$results = $html('td[style + width + height + !align]');

The lib takes care of pretty much everything.

https://code.google.com/p/ganon/

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