【问题标题】:URLencoding doesn't workURL编码不起作用
【发布时间】:2013-02-15 08:34:40
【问题描述】:

$words 变量是西班牙语单词,我的页面会从 www.rae.es 中查找它们的含义。 一些西班牙语单词有重音(á é ú í ó)。如果用户输入“baúl”,它不会识别它。我知道我应该使用函数 url_encode($words) 将其编码为“ ba%FAl ”(有效),但它不起作用。以下是大部分 PHP 代码:

    <?php

    // create an array of requests that we want
    // to load in the url.
    $words = array('word','word0','word1','word2','word3','word4','word5');

    function url_encode($string){
    return urlencode(utf8_encode($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">
    //bla bla bla
    </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 . $_REQUEST[$word]);

    // replace the data defined above.
    $contents = str_replace('</head>', $cssReplace, $contents);
    $contents = str_replace($spanRemove,"", $contents);

    // print out the result with the result index.
    // ++$resultIndex simply returns the value of 
    // $resultIndex after adding one to it.
    echo '<div id="result" style="
      margin-top:-80px;
      overflow:scroll; 
      width:800px; 
      height:150px;
      border: 1px solid #000000;
      border-radius: 15px;
      background-opacity: 0.5;
      background: #047C8F;
      -webkit-border-radius: 15px;
      -moz-border-radius: 15px;
      box-shadow: inset 0px 3px 13px #000000;
      -moz-box-shadow:
                   0px 3px 13px rgba(000,000,000,0.5),
                   inset 0px 0px 13px rgba(0,0,0,1);
      -webkit-box-shadow:
                   0px 3px 13px rgba(000,000,000,0.5),
                   inset 0px 0px 13px rgba(0,0,0,1);
     ', (++$resultIndex) ,'">', $contents ,
        '</div>',
        '<br/>',
        '<br/>',
        '<br/>',
        '<br/>',
        '<br/>';
        }
        ?>

关注样式替换上面的代码

谢谢!!

【问题讨论】:

  • 你的意思是它不起作用?您实际上是在调用 url_encode 吗?看起来不像。

标签: php html parsing variables encode


【解决方案1】:

你必须调用 url_encode...

改变这个

$contents = file_get_contents($baseUrl . $_REQUEST[$word]);

进入这个

$contents = file_get_contents($baseUrl . url_encode($_REQUEST[$word]));

【讨论】:

  • 致命错误:在第 588 行的 /Applications/XAMPP/xamppfiles/htdocs/IBProject/verbumpost.php 中调用未定义函数 url_encode()
  • 上面的代码适用于您的示例。如果声明在另一个文件中,您必须确保包含 url_encode 函数
  • 太棒了!我在哪里可以下载声明了函数的文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多