-1

感谢 stackoverflow 的人,他们帮助我实现了使用 Ajax 的功能,但现在我遇到了另一个问题。下面是代码:

if(isset($_POST['site'])){
if($_POST['site'] == NULL)
{
echo "Field cannot be empty";
return;
}

//here we check site validation on server side 
if (!preg_match("~^(?:(?:https?|ftp|telnet)://(?:[a-z0-9_-]{1,32}".
"(?::[a-z0-9_-]{1,32})?@)?)?(?:(?:[a-z0-9-]{1,128}\.)+(?:com|net|".
"org|mil|edu|arpa|gov|biz|info|aero|inc|name|[a-z]{2})|(?!0)(?:(?".
"!0[^.]|255)[0-9]{1,3}\.){3}(?!0|255)[0-9]{1,3})(?:/[a-z0-9.,_@%&".
"?+=\~/-]*)?(?:#[^ '\"&<>]*)?$~i", $_POST['site']))
{
echo "<br/>";
echo "<div id='err'>Oooops!!! Wrong site name</div>";
return;
} 

$homepage = file_get_contents("http://".$_POST['site']);
preg_match('%<meta.*name="keywords".*content="(.*)"\s+/>%U', $homepage, $regs);



if(count($regs))
 {
$myString = implode( '', $regs );  
 echo "<br/>";
 echo "<div id='test'>Keywords:</div>";
 print_r( "<div id='test2'>$myString</div>");
}
else
{
echo "<br/>";
echo "<div id='test'>There is no keywords</div>";
}
}


?>
<html>

<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>    
<script type="text/javascript">
$(document).ready(function(){
     $('#payment').submit(function(e){
        e.preventDefault();
        var x = $('input[name=site]').val();

       $.ajax({
             type: "POST",
            url: 'test.php',
            //data: $(this).serialize(),
            data: x,
            datatype: 'json',
            success: function(data)
            {
                 $('#metaTags').text(data)
            },
             error:   function(xhr, ajaxOptions, thrownError) { alert(xhr.status);} 

        });
    });
});
</script>


</head>     

<div>
<form id="payment"    method="post" name="forma1">
    <label for=name>ENTER www.bbc.com:</label>
    <input id="name" type=text placeholder="Write here..."         name="site">
    <input type="submit" value="START" name="searchbutton" id="sb">
</form>

<div id="metaTags"></div>

</div>  


</html> 

一切正常,但您可以在此处看到输出: http ://tsite.500mb.net/test.php 用于测试类型,例如:www.bbc.com,您将看到输出结果我需要 otput 将只有元标记,没有源代码。怎么做?我尝试了下一个想法:

var x = $('input[name=site]').val();
 $.ajax({
             type: "POST",
            url: 'test.php',
    data: x,

其中 data = x 和 x - 来自输入文本框的值,但它没有帮助。有任何想法吗?

4

1 回答 1

0

您可以使用get_meta_tags()函数,这是一个快速而肮脏的示例,您可以进一步了解。

php

<?

if (isset($_POST['site'])) {

    if (empty($_POST['site'])) {

        echo "<div>Field cannot be empty</div>";

    } else {

        //here we check site validation on server side
        if (!preg_match("~^(?:(?:https?|ftp|telnet)://(?:[a-z0-9_-]{1,32}" .
                "(?::[a-z0-9_-]{1,32})?@)?)?(?:(?:[a-z0-9-]{1,128}\.)+(?:com|net|" .
                "org|mil|edu|arpa|gov|biz|info|aero|inc|name|[a-z]{2})|(?!0)(?:(?" .
                "!0[^.]|255)[0-9]{1,3}\.){3}(?!0|255)[0-9]{1,3})(?:/[a-z0-9.,_@%&" .
                "?+=\~/-]*)?(?:#[^ '\"&<>]*)?$~i", $_POST['site'])) {

            echo "<div id='err'>Oooops!!! Wrong site name</div>";
        }

        $metatags = get_meta_tags("http://" . $_POST['site']);

        /* List all meta tags
        echo "<pre>".print_r($metatags, true)."</pre>";
        */

        if (count($metatags)) {
            foreach ($metatags as $tag => $value) {
                echo "<div class=\"result\"><strong>$tag:</strong> $value</div>";
            }
        } else {
            echo "<div id='test'>There is no keywords</div>";
        }
    }

    return;
}

?>

html

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#payment').submit(function(e) {
                e.preventDefault();
                var x = $('input[name=site]').val();

                $.ajax({
                    type: "POST",
                    url: 'test.php',
                    data: {site: x},
                    success: function(response) {
                        $('#metaTags').html(response)
                    },
                    error:   function(xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                    }
                });
            });
        });
    </script>

    <style>
        .result {
            margin-bottom: 15px;
        }
    </style>
</head>

<body>
    <div>
        <form id="payment" method="post" name="forma1">
            <label for="name">ENTER www.bbc.com:</label>
            <input id="name" type=text placeholder="Write here..." name="site">
            <input type="submit" value="START" name="searchbutton" id="sb">
        </form>

        <div id="metaTags"></div>
    </div>
</body>
</html>

希望能帮助到你。

于 2012-08-30T16:50:10.293 回答