需要帮助更换和与 &;

发布于 2024-09-30 20:57:07 字数 1261 浏览 2 评论 0原文

我不知道为什么我无法将 & 替换为 &

这是我的字符串:/Designers/batlak&selvig.jpg.

这是我到目前为止所尝试过的:

$image = preg_replace("#(&(?!amp;))#U",'&',$brand['image']);
$image = str_replace('&','&',$brand['image']);
$image = htmlspecialchars($brand['image']);
$image = mysql_real_escape_string($brand['image']);
$image = urlencode($brand['image']); // This works, but image will not show
$image = rawurlencode($brand['image']);  // This works, but image will not show

有什么建议吗?

更新

是的,它是 标记使用的图像网址的一部分。
urlencode 不起作用 - 图像标签不理解格式。

这是我在 W3C 验证器中遇到的错误:

Validation Output: 1 Error 
Line 84, Column 104: & did not start a character reference. 
(& probably should have been escaped as &.)

更新 2

这就是我测试它的方式:

<?php 
   $image = str_replace('&','&amp;',$brand['image']); 
   echo $image; 
?>

输出:
/Designers/batlak&selvig.jpg

我还测试了用 /Designers/batlak&selvig.jpg 替换 $brand['image'] >

I have no idea why I'm not able replace & with &

This is my string: /Designers/batlak&selvig.jpg.

This is what I've tried so far:

$image = preg_replace("#(&(?!amp;))#U",'&',$brand['image']);
$image = str_replace('&','&',$brand['image']);
$image = htmlspecialchars($brand['image']);
$image = mysql_real_escape_string($brand['image']);
$image = urlencode($brand['image']); // This works, but image will not show
$image = rawurlencode($brand['image']);  // This works, but image will not show

Any suggestions?

update

Yes, it's a part of an image url used by the <img> tag.
urlencode does not work - the image tag does not understand the format.

This is the error I get at W3C Validator:

Validation Output: 1 Error 
Line 84, Column 104: & did not start a character reference. 
(& probably should have been escaped as &.)

UPDATE 2

This is how I'm testing it:

<?php 
   $image = str_replace('&','&',$brand['image']); 
   echo $image; 
?>

Output:
/Designers/batlak&selvig.jpg

I've also tested replacing $brand['image'] with /Designers/batlak&selvig.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回首观望 2024-10-07 20:57:08

Firebug 会“自动隐藏”HTML 实体。

因此,请运行并粘贴原始(查看源代码、复制、粘贴)结果:

var_dump($brand['image']);
var_dump(urlencode($brand['image']));
var_dump(htmlspecialchars($brand['image']));

我的猜测是:

$image = htmlspecialchars(urlencode($brand['image']));

问题显然是您的文件名中有一个&符号,这很棘手;)

Firebug does "auto-hide" HTML entities.

So please run and paste the raw (View Source, Copy, Paste) result:

var_dump($brand['image']);
var_dump(urlencode($brand['image']));
var_dump(htmlspecialchars($brand['image']));

My guess would be:

$image = htmlspecialchars(urlencode($brand['image']));

The problem is appearantly that you have an ampersand in your filename which is... tricky ;)

坠似风落 2024-10-07 20:57:07

对我有用:

echo str_replace('&', '&', '/Designers/batlak&selvig.jpg');

// output:
Designers/batlak&selvig.jpg

也许您将其放入 href 或 src 中,并在浏览器状态/位置栏中转义。如果你查看源代码,它会被正确转义。

Works for me:

echo str_replace('&', '&', '/Designers/batlak&selvig.jpg');

// output:
Designers/batlak&selvig.jpg

Perhaps you are throwing this into an href or src where it is escaped in the browser status/location bar. If you look in the source code, it will be properly escaped.

掩耳倾听 2024-10-07 20:57:07

对于 URL:

$image = urlencode($brand['image'])

对于您的问题:

preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&',$brand['image']);

替换所有 '&' 而不替换任何现有的 '&' 字符。

For a URL:

$image = urlencode($brand['image'])

For your question:

preg_replace('/&(?![A-Za-z0-9#]{1,7};)/','&',$brand['image']);

That replaces all '&' without replacing any existing '&' characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文