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

标签 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/>';但它不起作用,我也不知道在哪里/如何使用它。

最佳答案

好吧,我将使用 jQuery 作为示例,因为如果您是编程新手,那么完成这项工作将是最容易的。

注意:我不建议在学习 JAVASCRIPT 之前使用 JQUERY - 这样做的风险由您自己承担,但至少稍后再回来学习 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;
?>

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

关于php - 如何打开在div中解析的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15914945/

相关文章:

php - .htaccess重写规则异常

javascript - 即使 Promise 没有实现,Promise 也会执行

html - 如何使html数据列表溢出?

javascript - 如何使用 JQuery 将新的 TR 插入 HTML 表格的中间部分?

javascript - DOM Element 是通过 JS 注入(inject)的吗?

php - 如何在MySQL 5中选择一个类别和子类别中的所有文章

php - 循环遍历3个不同的数组,并将值发送到MYSQL表中

php spl_autoload 不适用于语法错误的命名空间

javascript - 选中/取消选中 parent 和 child

javascript - 使用 html5 导出按钮 meteor 表格导出数据