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

Viewed 3629
<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.

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.

What seems to be the problem here?

Thank you

3 Answers

The white space is not allowed in urls, you need to replace it with %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

well the W3C validation service will complain about the absence of doctype declaration and the title element and the lang attribute

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.

The value of src includes a space which is not allowed as legal value. 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.
But you likely be better off using the PHP function rawurlencode() as it will encode anything else that should also be encoded.

Use rawurlencode() on the base name of the file name, or in your case, each directory name. This will transform spaces to %20, among other things.

You don't want to include the directory seporators '/' in the call to rawurlencode() as they will also get transformed and not work correctly.

Related