2013-03-13 30 views
1

我正在构建在线西班牙语字典。我从另一个网站合法获取定义。对于用户可以搜索的特定词语,提出了一系列建议。 如果您单击任何建议的单词,将弹出错误页面:http://verbum.xtrweb.com/verbumpost.php?word=boedo&word0=hola。我怎样才能让这些列出的单词通过下面粘贴的代码运行:(1)检索定义(2)改变样式。 我知道每一个建议词(检查元素)都有一个唯一的URL。如果粘贴在一起,与原网站的URL参数的代码,你会得到什么,我需要:复制Word并将其保存为变量

http://lema.rae.es/drae/srv/search?id=AVNYdnea1DXX2EH9E2mb

达“SRV /”所属网站

剩下的就是从具体字(在这种情况下,第一个建议,“bordar”

的过程:

<?php 
    $words = array('word','word0','word1','word2','word3','word4','word5','word6','word7','word7','word9',  
    'word10','word11','word12', 
    'word13','word14','word15');       
    function url_decode($string){ 
    return urldecode(utf8_decode($string)); 
    } 

    // we'll use this later on for loading the files. 
    $baseUrl = 'http://lema.rae.es/drae/srv/search?val='; 

    // string to replace in the head. 
    $cssReplace = <<<EOT 

    <style type="text/css"> 
      //blabla 
    </style> 
    </head> 
    EOT; 

    // string to remove in the document. 
    $spanRemove = '<span class="f"><b>.</b></span>'; 
    $styleRemove = 

    // use for printing out the result ID. 
    $resultIndex = 0; 

    // loop through the words we defined above 
    // load the respective file, and print it out. 
    foreach($words as $word) { 
// check if the request with 
// the given word exists. If not, 
// continue to the next word 
if(!isset($_REQUEST[$word])) 
    continue; 

// load the contents of the base url and requested word. 
$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word]))); 
// replace the data defined above. 
$contents = str_replace('</head>', $cssReplace, $contents); 
$contents = str_replace($spanRemove,"", $contents); 

$data = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/\1', $data); 

// print out the result with the result index. 
// ++$resultIndex simply returns the value of 
// $resultIndex after adding one to it. 
echo "<div id='results' style=' 
     //bla bla 
     } 
     ?> 

回答

1

我不认为我明白你的问题,但我看到你在一个未初始化的变量 - $ data上调用preg_replace。

也许你想要这个呢?

$data = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/\1', $contents); 

就像我说的,我没有完全理解你的问题,但我确实认为这是一个潜在的问题。

编辑:

从您的评论,它看起来像要追加链接的href被点击的基本URL,并请求页面的数据。你可以使用jQuery吗?像这样:

var baseUrl = "http://lema.rae.es/drae/srv/"; // base url 

//click function that binds to all anchors within a list item within a ul. in order 
//to get more precise, you may need to add some classes or ids 
$('ul li a').click(function(){ 
    var src = $(this).prop('href'); //this grabs the href property of the anchor 
    $(this).find('span').css('propery', 'change to this'); //change css property of span child of this anchor 
    //or if you want to add a class with styles to the element 
    $(this).find('span').addClass('class name'); 

    //ajax request to get page data 
    $.ajax({ 
     type: 'POST', 
     url: baseUrl+src //appending the query to the base url 
    }).done(function(data){ 
     //append the returned html to a div, or any element you desire 
     $('#myelement').append(data); 
    }); 
}); 

希望这会有所帮助。如果你需要jQuery的帮助,请告诉我。

+0

我需要从建议的单词中检索href值...我只是不知道如何 – j1nma 2013-03-14 03:50:08

+1

我做了编辑。让我知道这是否有帮助 – welbornio 2013-03-14 05:20:04

相关问题