函数突然返回一个完整的路径,前面有 dir 名称


Function all of a sudden return a complete path with the dir name in front?

我有一个奇怪的问题。我正在制作我自己的框架,就像codeigniter一样,我已经制作了一个等于CI的"base_url(("的函数,在我的例子中,它返回一个字符串:"http://www.example.com/"。我的问题(我以前从未听说过(在于当我使用 base_url(( 函数链接到 css 文件和其他导航时。因此,当我在视图中写道:

<link rel="stylesheet" type="text/css" href="<?= base_url(); ?>css/style.css" />

实际链接应为:

<link rel="stylesheet" type="text/css" href="http://www.example.com/css/style.css" />

我现在的问题是,当我查看Chrome和Firefox中的源代码时,从函数返回的"字符串"是正确的。但是当我将鼠标悬停在它上面时,链接会链接到以下网址:

http://example.com/%EF%BB%BFhttp://www.example.com/css/style.css

谁能解释为什么它会这样做?

编辑:很抱歉我忘记了 base_url(( 的源代码:

function base_url($url_arguments = array()){
  // Require config fil
  include(dirname(__FILE__).'/../system/config.php');
  // Generate link
  $return_url = $config['base_url']; // http://www.example.com/
  if(count($url_arguments) > 0){
    $return_url .= "?";
    foreach($url_arguments as $get => $value){
      $return_url .= $get."=".$value.'&';
    }
    preg_match("/(.+?)&$/i", $return_url, $matches);
    $return_url = $matches[1];
  }
  // Return link
  return ($return_url);
}

另外:我的同事在 VIM 中发现链接前面附加了一个名为 <feff> 的标签?

EFBBBF 是 UTF-8 字节顺序标记。一个以某种方式滑入了您的配置变量。编辑配置文件并将其删除。如果您正在从文件中读取变量,并且它在那里是合法的,请在代码中将其修剪掉。

我会把这个函数写成这样:

function base_url ($url_arguments = array()) {
  // Require config file
  include(dirname(__FILE__).'/../system/config.php');
  // Generate link
  $return_url = $config['base_url'];
  // Add query string, if any
  if (count($url_arguments)) {
    if (function_exists('http_build_query')) {
      $return_url .= "?".http_build_query($url_arguments);
    } else {
      $qStr = array();
      foreach($url_arguments as $key => $value) {
        $qStr[] = url_encode($key)."=".urlencode($value);
      }
      $return_url .= "?".implode('&',$url_arguments);
    }
  }
  // Return link
  return $return_url;
}

请注意丢失毫无意义的正则表达式 - 我怀疑这是以某种方式导致您的问题的原因。您可以简单地执行此操作$return_url = rtrim($return_url,'&');.