如何打开在div中解析的链接?

时间:2013-04-10 00:31:08

标签: php javascript html parsing html-parsing

我的网站解析西班牙字典,让您一次搜索多个字词。如果你查找“hola”,你会得到第一个div。有些词提出了建议,比如“casa”,而不是像“hola”这样的定义:

enter image description here

我正在寻找的是:

enter image description here

所以,我想:当你点击建议时(比如我发布的例子中的CASAR)将结果打印在像HOLA这样的div中。以下是使用的代码:

$words = array('word0','word-1');
    function url_decode($string){
    return urldecode(utf8_decode($string));
    }

    $baseUrl = 'http://lema.rae.es/drae/srv/search?val=';

    $cssReplace = <<<EOT

    <style type="text/css">
    // I changed the style
    </style>
    </head>
 EOT;

 $resultIndex = 0;

foreach($words as $word) {
if(!isset($_REQUEST[$word]))
    continue;

$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));

$contents = str_replace('</head>', $cssReplace, $contents);
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents);


echo "<div style='
      //style
     ", (++$resultIndex) ,"'>", $contents,
        "</div>";
} 

我尝试过:$contents .= '<a href="">' . $word . '</a><br/>';但它不起作用,我也不知道在哪里/如何使用它。

1 个答案:

答案 0 :(得分:1)

好的,我将使用jQuery作为示例,因为如果您不熟悉编程,这将是最容易完成工作的。

注意:我不建议使用JQUERY -BEFORE-学习JAVASCRIPT - 自己承担风险,但至少要回来并学习JAVASCRIPT

首先,阅读如何下载和安装jquery here

其次,你会想要一些像这样的东西,让我们假装这是你的标记。

<div id="wrapper">
    <!-- This output div will contain the output of whatever the MAIN
         word being displayed is, this is where HOLA would be from your first example -->
    <div id="output">

    </div>

    <!-- This is your suggestions box, assuming all anchor tags in here will result in
         a new word being displayed in output -->
    <div id="suggestions">

    </div>
</div>

<!-- Le javascript -->
<script>
    // Standard jQuery stuff, read about it on the jquery documentation
    $(function() {
        // The below line is a selector, it will select any anchor tag inside the div with 'suggestions' as identifier
        $('#suggestions a').click(function(e) {
            // First, stop the link going anywhere
            e.preventDefault();

            // Secondly, we want to replace the content from output with new content, we will use AJAX here
            // which you should also read about, basically we set a php page, and send a request to it
            // and display the result without refreshing your page
            $.ajax({
                url: 'phppage.php',
                data: { word: 'casar' },
                success: function(output) {
                    // This code here will replace the contents of the output div with the output we brought back from your php page
                    $('#output').html(output);
                }
            })
        });
    })
</script>

希望这些评论会有所启发,你需要设置你的php脚本,这将发送一个GET请求。 (例如,http://some.address.com/phppage.php/?word=casar

然后你只是回显PHP的输出

<?php

    $word = $_GET['word'];
    // Connect to some database, get the definitions, and store the results
    $result = someDatabaseFunctionThatDoesSomething($word);
    echo $result;
?>

希望这会有所帮助,我希望你有很多阅读要做!