how to skip InnerText empy in a If statment using php

89 Views Asked by At

I am trying to skip when InnerText is empty but it put a default value.

This is my code:

 if (strip_tags($result[$c]->innertext) == '') {
        $c++;
        continue;
    }

This is the output:

output screen

Thanks

EDIT2: I did the var_dump

 var_dump($result[$c]->innertext)

and I got this: var dump screen

how can I fix it please?

EDIT3: This is my code; I extract in this way the names of the teams and the results, but the last one not works in the best way when We have postponed matches

    <?php
include('../simple_html_dom.php');
 
function getHTML($url,$timeout)
{
       $ch = curl_init($url); // initialize curl with given url
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       return @curl_exec($ch);
}
 
$response=getHTML("https://www.betexplorer.com/soccer/japan/j3-league/results/",10);
$html = str_get_html($response);
 
$titles = $html->find("a[class=in-match]"); // name match
$result = $html->find("td[class=h-text-center]/a"); // result match
 
$c=0; $b=0; $o=0; $z=0; $h=0; // set counters
 
foreach ($titles as $match) {   //get all data
 
 
    $match_status = $result[$h++];
 
    
    if (strip_tags($result[$c]->innertext) == 'POSTP.') {  //bypass postponed match but it doesn't work anymore
        $c++;
        continue;
    }
 
 list($num1, $num2) = explode(':', $result[$c++]->innertext); // <- explode
    $num1 = intval($num1);
    $num2 = intval($num2);
    $num3 = ($num1 + $num2);
    $risultato = ($num1 . '-' . $num2);
   
    list($home, $away) = explode(' - ', $titles[$z++]->innertext); // <- explode
 
$home = strip_tags($home);
$away = strip_tags($away);
$matchunit = $home . ' - ' . $away;
 
                            echo "<tr><td class='rtitle'>".
                            "<td> ".$matchunit.  "</td> / " .  // name match
                            "<td class='first-cell'>" . $risultato . "</td> " .
                            "</td></tr><br/>";
        
        }   //close foreach     
?>
1

There are 1 best solutions below

1
Monnomcjo On

By browsing the content of the website you will always be dependent on the changes made in the future.

However, I will use PHP's native libxml DOM extension.

By doing the following:

<?php
function getHTML($url,$timeout)
{
       $ch = curl_init($url); // initialize curl with given url
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       return @curl_exec($ch);
}

$response=getHTML("https://www.betexplorer.com/soccer/japan/j3-league/results/",10);

// "Create" the new DOM document
$doc = new DomDocument();

// Load HTML from a string, and disable xml error handling
$doc->loadHTML($response, LIBXML_NOERROR);

// Creates a new DOMXPath object
$xpath = new DomXpath($doc);

// Evaluates the given XPath expression and get all tr's without first line from table main
$row = $xpath->query('//table[@class="table-main js-tablebanner-t js-tablebanner-ntb"]/tr[position()>1]');

echo '<table>';

// Parse the values in row
foreach ($row as $tr) {
    
    // Only get 2 first td's
    $col = $xpath->query('td[position()<3]', $tr);
    
    // Do not show POSTP and Round values
    if (!str_contains($tr->nodeValue, 'POSTP') && !str_contains($tr->nodeValue, 'Round')) {
        echo '<tr><td>'.$col->item(0)->nodeValue.'</td><td>'.$col->item(1)->nodeValue.'</td></tr>';
    }
}

echo '</table>';

You obtain:

<tr><td>Nagano - Tegevajaro Miyazaki</td><td>3:2</td></tr>
<tr><td>YSCC - Toyama</td><td>1:2</td></tr>
...