1

我正在建立一个在线西班牙语词典。我从另一个站点合法地获得了定义。对于用户可能搜索的特定单词,会出现一个建议列表。如果您单击任何建议的单词,则会弹出错误页面:http: //verbum.xtrweb.com/verbumpost.php?word=boedo&word0= hola。我怎样才能使这些列出的单词通过下面粘贴的代码运行:(1)检索定义(2)更改样式。我知道每个建议的单词(检查元素)都存在一个唯一的 URL。如果您将该代码与原始站点的 URL 参数粘贴在一起,您将得到我需要的内容。:

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
      }
        ?> 
4

1 回答 1

1

I don't think I understand your question, but I do see that you are calling preg_replace on an uninitialized variable - $data.

Maybe you want this instead?

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

Like I said, I don't fully understand your question, but I do see that as a potential problem.

EDIT:

From your comment, it looks like you want to append the href of the link being clicked to a base url and request that page's data. Can you use jQuery? Like this:

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);
     });
});

Hope this helps. If you need any help with jQuery just let me know.

于 2013-03-13T18:24:50.023 回答