在下面的脚本中,我有一个数组。我的数组存储来自网页的所有链接、标题和描述。但我想确保如果没有描述,它将使用 ap 标签的前 20 个字符,使用一个有效的功能。唯一的问题是我有拼图,似乎无法将它们放在一起,所以我希望我的 if 语句显示,如果描述为空,则使用函数 getWord 而不是getMetas().
function getMetas($link) {
  $str1 = file_get_contents($link);    
  if (strlen($str1)>0) {
    preg_match_all( '/<meta.*?name=("|\')description("|\').*?content=("|\')(.*?)("|\')/i', $str1, $description);
    if (count($description) > 1) {
      return $description[4];   
    }
  }
}
然后我的函数在这里(get_custom_excert),但没有必要看到它,因为我知道它有效。
function getWord() {
  $html = file_get_contents($link);    
  preg_match('%(<p[^>]*>.*?</p>)%i', $html, $re);
  $res = get_custom_excerpt($re[1]);
}
$outputs = array();
foreach ($links as $thisLink) {
  $output[] = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink));
  if ($output['description'] == null) {
  $output['description'] = getWord($res);
  }
  $outputs[] = $output;
}
print_r($output);