0

This script works well unless you put two URLS in that do not have meta tags, then they render in html all wrong.

How can I use the else statement in it so it will stop?

If you want to test it go here: http://php-playground.co.cc/testdir/metaex.php

<form method="get" action=<?php echo "'".$_SERVER['PHP_SELF']."'";?> >
<p>URL of Competitor:</p> 
<textarea name="siteurl" rows="10" cols="50">
<?php //Check if the form has already been submitted and if this is the case, display the     submitted content. If not, display 'http://'.
echo (isset($_GET['siteurl']))?htmlspecialchars($_GET['siteurl']):"http://";?>
</textarea><br>
<input type="submit" value="Submit">
</form>
<div id="nofloat"></div>
<table>
<?php
function parseUrl($url){
    //Trim whitespace of the url to ensure proper checking.
    $url = trim($url);
    //Check if a protocol is specified at the beginning of the url. If it's not,    prepend 'http://'.
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
            $url = "http://" . $url;
    }
    //Check if '/' is present at the end of the url. If not, append '/'.
    if (substr($url, -1)!=="/"){
            $url .= "/";
    }
    //Return the processed url.
    return $url;
}
//If the form was submitted
if(isset($_GET['siteurl'])){
    //Put every new line as a new entry in the array
    $urls = explode("\n",trim($_GET["siteurl"]));
    //Iterate through urls
    foreach ($urls as $url) {
            //Parse the url to add 'http://' at the beginning or '/' at the end if not   already there, to avoid errors with the get_meta_tags function
            $url = parseUrl($url);
            //Get the meta data for each url
            $tags = get_meta_tags($url);
            //Check to see if the description tag was present and adjust output   accordingly
            echo (isset($tags['description']))?"<tr><td>Description($url)</td>  <td>".$tags['description']:"Description($url)</td><td>No Meta Description</td></tr>.";
    }
}
?>
</table>

Thanks very much!

1
  • The part I need help with is echo'ing the table :) Commented Apr 11, 2012 at 14:14

2 Answers 2

1

First, remove the last dot . in the line :

echo (isset($tags['description']))?"<tr><td>Description($url)</td> <td>".$tags['description']:"Description($url)</td><td>No Meta Description</td></tr>.";

EDIT :
I haven't seen this but you have one more error in this line :
Replace ".$tags['description']:" by ".$tags['description'].":

Sign up to request clarification or add additional context in comments.

Comments

0

multiple ways to do this; why don't you use simpler way to do this

$tags = NULL;
$tags = get_meta_tags($url);
if($tags)
echo "<tr><td>Description($url)</td><td>" .$tags['description']. "</td></tr>";
else 
echo "<tr><td>Description($url)</td<td>No Meta Description</td></tr>";

or if you want to stick with your code try this, need to have staring and ending tags for both true and false;

echo (isset($tags['description'])) ? '<tr><td>Description($url)</td><td>' . $tags['description'] . '</td></tr>' : '<tr><td>Description($url)</td><td>No Meta Description</td></tr>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.