函数突然返回前面带有目录名称的完整路径?
我有一个奇怪的问题。我正在制作类似 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 中发现一个名为
的标签附加在链接前面?
I have a weird problem. I am making kinda my own framework like codeigniter and i have made a function equal to CI's "base_url()" which in my case returns a string like: "http://www.example.com/". My problem (which i have never heard off before) lies when I use my base_url() function to make links to css files and other navigation. So when I, in a view writes:
<link rel="stylesheet" type="text/css" href="<?= base_url(); ?>css/style.css" />
the actual link should be:
<link rel="stylesheet" type="text/css" href="http://www.example.com/css/style.css" />
My problem now is that the "string" returned from the function is correct when i look at the source code in Chrome and Firefox. But when i hover my mouse over it, the link, links to the following url instead:
http://example.com/%EF%BB%BFhttp://www.example.com/css/style.css
Can anyone explain why it would do this?
EDIT:
Im so sorry i forgat the source code for 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);
}
Extra: my co-worker found in VIM that a tag named <feff>
is appended in front of the link?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
EFBBBF 是 UTF-8 字节顺序标记。一个已经以某种方式溜进了你的配置变量。编辑配置文件并将其删除。如果您正在从文件中读取变量并且该变量存在是合法的,请在代码中将其删除。
EFBBBF is the UTF-8 byte order mark. One has slipped into your config variable somehow. Edit the config file and remove it. If you're reading the variable from a file and it's legitimate for it to be there, trim it off in code.
我会这样编写该函数:
注意毫无意义的正则表达式的丢失 - 我怀疑这就是导致您问题的原因。您可以简单地使用
$return_url = rtrim($return_url,'&');
来完成此操作。I would write that function as this:
Note the loss of the pointless regex - I suspect this is what was somehow causing your problem. You could have done this as simply
$return_url = rtrim($return_url,'&');
.