Hi, I have written code to fetch all the FONT elements from the html below using DOMDocument and copy them into an array and remove all the empty elements. Everything works fine except that I don't know how to remove the empty elements from the array. Here is what I have:

<TABLE><TR><TD width="6%">&nbsp;</TD>
<TD><PRE ><FONT >
<TR>
<TD>
<FONT COLOR=000000>DT</FONT>
<FONT COLOR=000000>01</FONT>
<FONT COLOR=000000> </FONT>
<FONT COLOR=0000FF>03</FONT>
<FONT COLOR=0000FF> </FONT>
<FONT COLOR=000000>LL</FONT>
</FONT></PRE></TD>
</TR>
</TABLE>

$mastersched = file_get_contents('m2.html');

$dom = new DomDocument();
$dom->preserveWhiteSpace = false;
$dom->loadHTML($mastersched);

$x = new DomXPath($dom);

$nodes = $x->query('/html/body/table/tr/td[2]/pre/font/font');
$nodelength = $nodes->length;

foreach ($nodes as $node) {
$nodearr[] = $node; //copy node object to array
}

    I think there is possibly some smart array function
    that can easily do this: http://php.net/manual/en/ref.array.php

    But I would go for a simple [man]foreach[/man]
    You here can define what you regard as empty.
    For example this may look as empty.

    $string = "           ";

    But it is really a string with several space characters. And of a certain length.
    And $number = 0;
    is sometimes defined as EMPTY
    but it can also be the number '0'

    What I mean is, that there can be many ways to describe EMPTY.
    And in this code below, you can add whatever triggers the [man]unset[/man]

    I have here used [man]empty[/man], which is the way this php function tests for emptiness.

    <?php
    
    // remove all elements in $arrayname with value==empty
    
    foreach( $arrayname as $key=>$val ){
    	if( empty($val) ) unset( $arrayname[$key] );
    }
    
    ?>

      Thank you, but I added this:

      foreach ($nodearr as $key=>$val ) {

      if (empty($val)) unset($nodearr[$key] );
      echo $key.'->'.$val.'<br>';
      }

      The line echo $key.'->'.$val.'<br>'; keeps generating an error:
      Object of class DOMElement could not be converted to string

      So why is does PHP still think $nodearr is still an object when it was converted into an array with the code:

      foreach ($nodes as $node) {
      $nodearr[] = $node;
      }

      Thanks.

        You didn't convert anything... you just created an array of objects.

          ok, I give up. How do I convert an array of objects into an array of strings?

            The most straightforward approach would be to typecast them:

            foreach ($nodes as $node) {
            $nodearr[] = (string)$node;
            }

            But as you've already found, DOMElements have no inbuilt definition about what it means to be "converted into a string". Instead I'm guessing you want each node's text content (trimmed of leading and trailing whitespace), skipping any other tags.

            foreach($nodes as $node)
            {
                $nodearr[] = trim($node->textContent);
            }

            The empty elements would all be empty strings now. Removing those from the array can be done in one statement:

            $nonempty = array_filter($nodearr, 'strlen');

            Only strings that have a nonzero length will be put into $nonempty.

              That works very nicely Weedpacket. Thanks a bunch for explaining.😃

                Write a Reply...