Handy Dynamic link to https://validator.w3.org/nu/

#1

'because I frequently check HTML validation to ensure my web-page has no errors I wrote the following script to generate the link which can be placed on any web-page.

<?php 
  ...
  ...

  $w3Url = 'https://validator.w3.org/nu/?doc=';

  $myUrl = $_SERVER['REQUEST_SCHEME']
          . '%3A%2F%2F'
          . $_SERVER['SERVER_NAME']
          . '' 
          . $_SERVER['REQUEST_URI']
          ;
  $params = '&amp;showoutline=yes'
          . '&amp;showsource=yes'
          ;

  $link  =  $w3Url
          . $myUrl
          . $params
          ;

// Usage:
   echo '<a href="' .$link .'"> HTML5 Validate </a>';

Any errors. warnings or improvements?

#2

I wouldn’t use variables in stuff like these to prevent overriding existing variables that are expected to exist after the snippet, causing all sorts of hard to track bugs.

I’d do it like this:

printf (
    '<a href="https://validator.w3.org/nu/?doc=%s&amp;showoutline=yes&amp;showsource=yes">HTML5 validate</a>',
    urlencode(sprintf('%s://%s%s', $_SERVER['REQUEST_SCHEME'], $_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']))
);
2 Likes
#3

The script is in a common footer and there are no following variables.

Normally I would create a function or method to reduce the script to a single line. All variables would then be local to the function.

I do like your alternative script and it does seem a lot more sensible. Well worth considering when I am writing my next project or when updating an existent project.