PHP: Check website for responsive design

时间:2016-04-07 10:39:17

标签: php responsive-design detection

is there a class/script/possibility to check websites for responsive design? It could be something like this:

if (is_responsive('http://www.site.tld')) {
   echo 'is responsive';
}
else {
   echo 'is not responsive';
}

Any ideas?

4 个答案:

答案 0 :(得分:7)

There are two possible* ways of doing this:

  1. You could use JavaScript to detect if there are any media queries in your CSS then send a true/false variable to PHP using Ajax

  2. Maybe even use your server-side language to read your CSS file and use RegEx to search for any media queries in the file.

These methods are not guaranteed to work and there is really no "standard", "safe" or "non-hackey" way of doing this, also you CAN NOT use server-side technology to directly handle media queries.

RegEx's that you can use to match @media rules are:

  1. ^\@media\s(.*?)\{$

    ^ = assert position at start of the string
    \@ = matches the character @ literally
    media = matches the characters media literally
    \s = match any white space character [\r\n\t\f ]
    .*? = matches any character (except newline)
    { = matches the character { literally
    $ = assert position at end of the string

  2. \@media\s(.*?)\{

    \@ = matches the character @ literally
    media = matches the characters media literally
    \s = match any white space character [\r\n\t\f ]
    .*? = matches any character (except newline)
    { = matches the character { literally

Also you can use the i to make the RegEx's case insensitive (i only ignores the case of [a-zA-Z])

Here are four possible ways of detecting @media rules using RegEx:

  1. /^\@media\s(.*?)\{$/
  2. /^\@media\s(.*?)\{$/i
  3. /\@media\s(.*?)\{/
  4. /\@media\s(.*?)\{/i

答案 1 :(得分:1)

There is no regular or perfect function to do this. For checking programmatically, you can parse the stylesheets for media queries.

So to be clear, it's not recommend to use it, since media queries can be used for a lot of things.

If you would like to try it anyway, this might help you: @media ?[^{]+?{.

答案 2 :(得分:1)

No technically you can't check responsiveness using PHP script as it's a client side thing.

答案 3 :(得分:0)

No! especially in PHP. You can try to parse CSS for media queries, or check if element sizes are set in absolute values or not.

In my opinion way to go is having some web engine running server side, and force it to render page with different window.width, and check if document width will change with it. Such module could be executed from php, but cannot be done in php. - and if you don't have dedicated server, forget trying such stunts.