I have an array.
In my array is the final piece of my script. My script scans all the links and puts in the array $link. Then it scans all the titles in the $link and then gets all the meta titles of the $link. But then some people don't have meta descriptions, so I decided to create a function which returns 20 characters of a p tag scanned.
Though in my array, my if statement doesn't seem to work. It is supposed to detect if there is any input in the 'description' key in the variable and then use the instructions in the if statement.
However, it doesn't seem to be doing that and I keep thinking I am missing something.
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];
}
}
}
function get_custom_excerpt($return, $option = 30, $sentance = false) {
$marks = Array(".","!","?");
$return = strip_tags($return);
if($sentance == true) {
$start = implode(" ", array_slice(preg_split("/\s+/", $return), 0, $option ));
$start .= ' ';
$end = implode(" ", array_slice(preg_split("/\s+/", $return), $option));
$cut = Array();
foreach($marks AS $m => $mark){
$mark = strpos($end, $mark);
if($mark != false) $cut[$m] = $mark;
}
if($cut[0] != "")
$chop = min($cut);
else
$chop = $option;
$rest = substr($end, 0, $chop);
$key = array_search($chop, $cut);
$return = $start.$rest;
}else{
$return = implode(" ", array_slice(preg_split("/\s+/", $return), 0, $option));
}
$return .= $marks[$key];
return $return;
}
$html = file_get_contents($link);
preg_match('%(<p[^>]*>.*?</p>)%i', $html, $re);
$res = get_custom_excerpt($re[1]);
$out = Array();
foreach ($links as $thisLink) {
$out = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink));
if (empty($out['description'])) {
$out['description'] = $res;
}
$output[] = $out;
}
By the way, the code works perfectly except the if statement. The function has been executed on a smaller array, not the array I am using, and it has returned results to my standard. But it seems to create another key ('description') instead of adding the p tag to it. Any ideas?