![](/img/trans.png)
[英]How can i transform my function into variable for executing statement array
[英]How can I make my function work with my if statement that changes my array
在下面的脚本中,我有一个数组。 我的数组存储了网页中的所有链接,标题和描述。 但是我想确保如果没有描述,它将使用有效的函数使用ap标签的前20个字符。 唯一的问题是我有拼图碎片,似乎无法将它们放在一起,因此,我希望我的if语句表明如果描述为空,请使用函数getMetas()
而不是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
),但由于我知道这是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);
这怎么样?
foreach ($links as $thisLink) {
$output = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink));
if ($output['description'] == null) {
$output['description'] = getWord($res);
}
$outputs[] = $output;
}
这是你想要的吗?
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];
} else {
return getWord($str1);
}
}
}
function getWord($html) {
preg_match('%(<p[^>]*>.*?</p>)%i', $html, $re);
return get_custom_excerpt($re[1]);
}
顺便说一句,用regexp解析HTML非常脆弱,最好使用DOM解析器库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.