2013-03-13 27 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

相關問題