0

此脚本运行良好,除非您将两个没有元标记的 URL 放入其中,否则它们在 html 中呈现都是错误的。

如何在其中使用 else 语句使其停止?

如果你想测试它去这里: 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>

非常感谢!

4

2 回答 2

1

首先,删除该.行中的最后一个点:

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

编辑:
我还没有看到这个,但你在这一行还有一个错误:
替换".$tags['description']:"".$tags['description'].":

于 2012-04-11T14:19:36.350 回答
0

多种方法可以做到这一点;你为什么不使用更简单的方法来做到这一点

$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>";

或者如果你想坚持你的代码试试这个,需要有真假的开始和结束标签;

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>';
于 2012-04-11T16:47:35.640 回答