RuFFCuT
RuFFCuT

Reputation: 317

Duplicating Meta Description when using get_meta_tags function, only want the description

When echo'ed out instead of just removing all of the other meta tags it seems to be duplicating the description, for example:

BBC has 13 different meta tags, when I echo out just the description in my script it is duplicating it 13 times.

<?php
//make the array 
$TAarray = explode("\n", strip_tags($_POST['TAData'])); 

foreach ($TAarray as $key => &$line) {
            $line = trim($line); 
            // get the meta data for each url
            $tags = get_meta_tags($line);

            echo '<tr>';
            foreach ($tags as $meta)        
            {
            echo (isset($tags['description']))?"<br><br       />\nDescription($line):<br>\n".$tags['description']:"<br>\nDescription($line):<br>\nNo Meta    Description.";
                    /*echo '<td>' . $meta . '</td>';*/
            }
            echo '</tr>';
    }

    ?>

Here is the URL incase anyone wanted to see it working: http://php-playground.co.cc/testdir/metaex.php

PS

I know the checkboxes are not working they are only there for the layout

Upvotes: 0

Views: 158

Answers (3)

hakre
hakre

Reputation: 197658

If you use foreach with references, it's good practice to remove that reference after the loop:

foreach ($TAarray as $key => &$line)
{
    $line = trim($line); 
}
unset($line); # remove the reference for safety reasons

But as you don't iterate over $TAarray after that code, the code is superfluous anyway. Don't write superfluous code. I suggest the following:

//make the array 
$TAarray = explode("\n", strip_tags($_POST['TAData'])); 
$TAarray = array_map('trim', $TAarray);

And I suggest you put that into a function of it's own:

/**
 * @param string $html
 * @return string[] lines
 */
function getTrimmedTextLinesArrayFromHTMLBlock($html)
{
    $text = strip_tags($html);
    $lines = explode("\n", $text);
    $trimmed = array_map('trim', $lines);
    return $trimmed;
}

You can then use it wherever you see fit. You can also test this function independently with different input:

$lines = getTrimmedTextLinesArrayFromHTMLBlock($_POST['TAData']));
$whitelist = array("description");
foreach ($lines as $line)
{
    if (! $tags = get_meta_tags($line)) continue;
    echo '<tr>';
    foreach ($tags as $key => $meta)
    {
        if (! in_array($key, $whitelist)) continue;
        echo '<td>' . $meta . '</td>';
    }
    echo '</tr>';
}

I hope this is helpful.

Upvotes: 0

Treffynnon
Treffynnon

Reputation: 21553

I think this is what you are trying to do:

<?php
//make the array 
$TAarray = explode("\n", strip_tags($_POST['TAData'])); 

foreach ($TAarray as $key => &$line) {
            $line = trim($line); 
            // get the meta data for each url
            $tags = get_meta_tags($line);

            echo '<tr>';
            echo (isset($tags['description']))?"<br><br       />\nDescription($line):<br>\n".$tags['description']:"<br>\nDescription($line):<br>\nNo Meta    Description.";
                   echo '<td>' . $tags['description'] . '</td>';
            echo '</tr>';
    }

    ?>

You'll note that have removed the second for loop.

Upvotes: 1

Quentin
Quentin

Reputation: 943185

You are looping over the meta tags, and for each meta tag you are echoing out the description.

Get rid of the loop.

Upvotes: 1

Related Questions