检查 DOM var 在 php 中是否为空


checking if a DOM var is empty in php

嗨,这是我在文件中的 html 代码:

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

我正在用这段代码从div 中提取文本:($file var 具有指向上述文件的正确路径(

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;

但是我无法检查其中一个div 是否为空,如果其中一个变量 ($title,$city,$country...( 为空,则隐式。(因为并不总是一个城市或一个头衔...

我试过:

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

有什么建议吗?

试试这个:

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

<title>标记为空时,if块将执行。