0

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?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Noah Smith
  • 203
  • 4
  • 9
  • 2
    Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost Sep 24 '12 at 22:23
  • Your inconsistent indentation makes it difficult to read your code. I've fixed it for you. – Keith Thompson Sep 24 '12 at 23:41
  • is my code clear on my intentions? – Noah Smith Sep 24 '12 at 23:43

1 Answers1

0

The problem is that you are adding arrays to the $output array, so when you try to access the array you just added, you are actually access the $output array as a whole. Try this:

foreach ($links as $thisLink) {
    $out = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink));

    if (empty($out['description'])) {
        $out['description'] = $res;
    }

    $output[] = $out;
}
Matt
  • 1,151
  • 3
  • 13
  • 34
  • it still seems to return an empty result – Noah Smith Sep 24 '12 at 22:22
  • There is probably something wrong with the getMetas() function. That code should work fine. Try echoing the $out['description'] and see what is really in there. – Matt Sep 24 '12 at 22:25
  • i am sure there is nothing wrong with my getMetas (), because the script works fine. Do you want me to show you the whole code, with the function and getMetas()? – Noah Smith Sep 24 '12 at 22:27
  • 1
    the answer should use `empty` on the if statement: `if(empty($out['description']))` – JimmyBanks Sep 24 '12 at 22:32
  • Your updated code is wrong, copy-paste the code I posted into your code. – Matt Sep 24 '12 at 23:11
  • The array code, probably. In the version you have right now, it is definitely the array code. – Matt Sep 25 '12 at 00:00