使用get_meta_tags函数时复制元描述,只需要描述

使用get_meta_tags函数时复制元描述,只需要描述

问题描述:

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("
", 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       />
Description($line):<br>
".$tags['description']:"<br>
Description($line):<br>
No 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

当回显而不仅仅删除所有其他元标记时,它似乎是重复描述, 例如: p>

BBC有13种不同的元标记,当我在我的脚本中只显示它的描述时,它会重复13次。 p>

 &lt;?php 
 //制作数组
 $ TAarray = explode(“
”,strip_tags($ _ POST ['TAData']));  
 
foreach($ TAarray as $ key =&gt;&amp; $ line){
 $ line = trim($ line);  
 //获取每个网址的元数据
 $ tags = get_meta_tags($ line); 
 
 echo'&lt; tr&gt;'; 
 foreach($ tags as $ meta)
 {
 \ echo  (isset($ tags ['description']))?“&lt; br&gt;&lt; br />
Description($line):<br>
".$tags['description']:"<  br&gt; 
描述($ line):&lt; br&gt; 
无元描述。“; 
 / * echo'&lt; td&gt;'  。  $ meta。  '&lt; / td&gt;'; * / 
} 
 echo'&lt; / tr&gt;'; 
} 
 
?&gt; 
  code>  pre> 
 
 

这是任何人都希望看到它工作的URL: http:// php-playground。 co.cc/testdir/metaex.php p>

PS p>

我知道复选框不起作用它们仅用于布局 p> div>

I think this is what you are trying to do:

<?php
//make the array 
$TAarray = explode("
", 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       />
Description($line):<br>
".$tags['description']:"<br>
Description($line):<br>
No Meta    Description.";
                   echo '<td>' . $tags['description'] . '</td>';
            echo '</tr>';
    }

    ?>

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

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

Get rid of the loop.

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("
", 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("
", $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.