简体   繁体   English

W3C 验证失败,路径段中有非法字符:不允许有空格

[英]W3C Validation Failed, Illegal character in path segment: space is not allowed

<img src="webpage photos/alcazar.jpg" alt="Alcazar Palace">

The error shown by validator: Bad value webpage photos/alcazar.jpg for attribute src on element img: Illegal character in path segment: space is not allowed.验证器显示的错误:元素 img 上属性 src 的值错误的网页照片/alcazar.jpg:路径段中的非法字符:不允许空格。

As soon as I remove the spaces I try to replace them by hyphens or %20 as recommended by some online I get even more errors but the webpage functions.删除空格后,我尝试按照某些在线建议的方式用连字符或 %20 替换它们,但网页功能会出现更多错误。

What seems to be the problem here?这里似乎有什么问题?

Thank you谢谢

The white space is not allowed in urls, you need to replace it with %20 url中不允许有空格,需要用%20替换

As soon as I remove the spaces I try to replace them by hyphens or %20 as recommended by some online I get even more errors删除空格后,我尝试按照某些在线建议的方式用连字符或 %20 替换它们,我会遇到更多错误

well the W3C validation service will complain about the absence of doctype declaration and the title element and the lang attribute W3C 验证服务会抱怨缺少doctype声明以及title元素和lang属性

so when you check with more code因此,当您检查更多代码时

<!DOCTYPE html>
<html lang="en-us">
<head>
  <title>Some title</title>
</head>
<body>
  <img src="webpage%20photos/alcazar.jpg" alt="Alcazar Palace">
</body>
</html>

you get Document checking completed. No errors or warnings to show.你完成Document checking completed. No errors or warnings to show. Document checking completed. No errors or warnings to show.

The value of src includes a space which is not allowed as legal value. src的值包含一个不允许作为合法值的空格。 please move out the image to another place without any space in path, and re-link to it again.请将图像移到另一个位置,路径中没有任何空格,然后重新链接到它。

You could use the PHP function str_replace() to replace the spaces with %20.您可以使用 PHP function str_replace()将空格替换为 %20。
But you likely be better off using the PHP function rawurlencode() as it will encode anything else that should also be encoded.但是你可能最好使用 PHP function rawurlencode()因为它会编码任何其他也应该被编码的东西。

Use rawurlencode() on the base name of the file name, or in your case, each directory name.在文件名的基本名称上使用rawurlencode() ,或者在您的情况下,每个目录名称。 This will transform spaces to %20, among other things.这会将空格转换为 %20 等。

You don't want to include the directory seporators '/' in the call to rawurlencode() as they will also get transformed and not work correctly.您不想在对rawurlencode()的调用中包含目录分隔符“/”,因为它们也会被转换并且无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM