简体   繁体   中英

PHP array only using the first line of text area for 'Get Meta Tags' Function

I am fairly new to PHP so please bear with me:)

What I am trying to do if place URL's into a text area, then pull in the meta data for each.

I have made the script, but when I place more then one URL into the text area it only returns data for the last URL entered, I thought maybe you guys can help me:)

<form method="POST">
<textarea name="TAData">
</textarea>
<input type="submit" value="submit"/>
</form>

<div id="checkboxes">
<input type="checkbox" name="vehicle" value="PR" /> Show me the PR<br />
<input type="checkbox" name="vehicle" value="KW Tag" /> Show me the KW tag<br />
<input type="checkbox" name="vehicle" value="Title Tag" /> Show me the Title tag<br />
</div>
<div id="checkboxes">
<input type="checkbox" name="vehicle" value="1stH1" /> Show me the 1st H1<br />
<input type="checkbox" name="vehicle" value="2ndH1" /> Show me the 2nd H1 tag<br />
<input type="checkbox" name="vehicle" value="SeedKW" /> Show me Seed KW's<br />
</div>

<div id="nofloat"></div>

<?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);

unset($tags["content-type"]);
unset($tags["page-type"]);
unset($tags["page-topic"]);
unset($tags["audience"]);
unset($tags["content-language"]);       

            echo '<tr>';
            foreach ($tags as $meta)        
            {
                    echo '<td>' . $meta . '</td>';
            }
            echo '</tr>';

?>

The closing } after where you use trim on the line means that the foreach ends and only the last line is available after the loop for the other operations. Just move that bracket to the end.

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']));
$blacklist= array("content-type", "page-type", "page-topic", 
                "audience", "content-language");
foreach ($lines as $line)
{
    if (! $tags = get_meta_tags($line)) continue;
    echo '<tr>';
    foreach ($tags as $key => $meta)
    {
        if (in_array($key, $blacklist)) continue;
        echo '<td>' . $meta . '</td>';
    }
    echo '</tr>';
}

I hope this is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM