ADU77
ADU77

Reputation: 35

checking if a DOM var is empty in php

Hi this is my html code in a file:

<HTML>
<HEAD>
<div id="title"> Title  </div>
<div id="city"> City </div>  
<div id="country">Country</div> 
<div id="company">Company </div>  
</TITLE>
.................

and I am extractig the text from div's with this code:(the $file var has the correct path to the upmentioned file)

include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file($file);

$title1 = $html->getElementById('title');
$title = $title1->innertext;

$city1 = $html->getElementById('city');
$city = $city1->innertext;

$country1 = $html->getElementById('country');
$country = $country1->innertext;

$company1 = $html->getElementById('company');
$company = $company1->innertext;

But I can't check if one of the div's is empty and implicit if one of the var ($title,$city,$country...) is empty. (as not always will be a city or a title...)

I have tried:

if(empty($title){...}
if(isset($title){...}
if(is_null($title){...}
if($title !== ""){...}

Any sugestions ?

Upvotes: 1

Views: 926

Answers (1)

Sazzadur Rahman
Sazzadur Rahman

Reputation: 2940

Try this:

if(!strlen(trim($title))) {
    // do something
}

The if block will execute when the <title> tag is empty.

Upvotes: 1

Related Questions