【问题标题】:Opening a link in a div with PHP使用 PHP 在 div 中打开链接
【发布时间】:2013-04-05 02:47:31
【问题描述】:

我正在构建一个解析西班牙语词典的网站。如果您查找单词 HOLA,您将收到定义,但换句话说,您会收到建议,例如 CASA:http://verbum.xtrweb.com/verbumpost.php?word0=hola&word-1=casa

我希望:当您单击建议时(例如我上面发布的示例中的 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>";
    }

我开始写代码了,请耐心等待,我也尝试了一些朋友推荐的DOM代码,但没有成功。

【问题讨论】:

  • 我认为你应该为你想要完成的事情寻找 API,而不是解析 HTML。
  • 最简单的方法是将结果加载到服务器调用的 div 中,但将其隐藏。然后根据点击更改可见性。您可以使用 AJAX,但它会有点矫枉过正,这里绝对不需要。

标签: php jquery html php-parser


【解决方案1】:

output content in a styled div,只需替换:

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

与:

$divStyle = 'background: none repeat scroll 20% center transparent; margin-left: 275px; height: auto; box-shadow: 0px 0px 10px rgb(0, 0, 0) inset; border: 1px solid rgb(0, 0, 0); margin-top: 35px; padding-left: 14px; width: 793px; padding-bottom: 80px;';

//insert $divStyle as the "style" attribute
echo "<div style='" . $divStyle . "'" . (++$resultIndex) . "'>" . $contents . "</div>";

改变了两件事:

  • 在 PHP 字符串中加入点而不是逗号。
  • 删除溢出(没有 需要滚动条)并添加填充(额外的内部空间)到 div 底部

【讨论】:

  • 哦,是的,忘记了,确保 HTML 文件具有 .php 扩展名,哈哈。还要确保php代码在php标签内,即:&lt;?php?&gt;
  • 听起来不错!我将在接下来的 10 个小时内休息,所以我无法回答您可能有的其他问题 :)。如果可行,请随时将此问题标记为已回答。
  • 真的吗?我会在几秒钟内在我的网站上给你一个活生生的例子
  • ah k,抱歉解决方案不正确,明天早上将发布另一个解决方案。虽然你想要的是exactly like this yeah?。如果是这样,您需要在上面发布的唯一代码就是最后一个 echo 语句。
  • 嘿,很高兴知道。查看我的编辑,您将能够知道如何使用我的新代码设置页面其余部分的样式。随时将此问题标记为已回答,干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
  • 2019-10-24
  • 1970-01-01
  • 2011-09-28
相关资源
最近更新 更多