John Davidson

php - Regex conditional statement or negation

0 comments
Message:


I'm trying to detect mobile with the user agent string. According MDN there is always the pattern "mobile" on mobiles user agent strings and not on PC, tablet or whatever; but with one exception the iPad's user agent string :(


Basicly I need this for (in)activate a JS function :


examples of user agent strings:
$str = 'Mozilla/5.0 (Linux; Android 10; Redmi Note 9S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36';
$str = 'Mozilla/5.0 (iPad; CPU OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/95.0.4638.50 Mobile/15E148 Safari/604.1';
$str = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0';

basic working code in php:
if(preg_match('#Mobile#i', $str)){
if(preg_match('#iPad#i', $str)){ $mobile = false; // (ipad)
} else { $mobile = true; //(mobile);
}
} else { $mobile = false; } //others

But:



  1. this is inelegant :D

  2. I want to improve my knowledge in Regexp, which at the moment I'm using in a simple way without the lookaround or conditionnal stuff.


So since yesterday I try to learn about lookaround and conditionnal, read a lot of tutorials, try a bunch of patterns but I still not find a solution or really understand the lookahead ou lookbehind patterns, neither forcing the match attempt to Fail with (*PRUNE)(*F)


$reg = '/(?(?<=iPad)|Mobile)/i';
$reg = '/(?(?<=Mobile)|(iPad))/i';
$reg='^(?(?=.*Mobile)(?:iPad|)).*$';
$reg='(?(?=Mobile)(?(?=iPad)(*F)|Mobile)|(*F))';
$reg='(?(?=Mobile)(?(?!iPad)(Mobile)|(*F))|(*F))'; //and variations lookbehind /lookahead (positive or negative)

As there is not conditional patterns within the JS regex engine , all I need is a boolean: false for "mobile" without the ipad, or true for the rest.
Why $reg='(?(?=Mobile)(?(?=iPad)(*F)|Mobile)|(*F))'; return 'mobile' and not fail ?
Is there an elegant solution ? Please help me ... I'm going nuts :D


Cheers.



My Answer:
"Mobile" in the user agent string if the device is a mobile device. You can use a regular expression with a conditional statement or negation to detect if the user agent string contains the word "Mobile".

Here is an example using a conditional statement in PHP:

php
$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (preg_match('/Mobile/i', $userAgent)) {
echo 'Mobile device detected';
} else {
echo 'Not a mobile device';
}


This code snippet checks if the user agent string contains the word "Mobile" (case-insensitive) and prints a message accordingly.

Alternatively, you can use negation to check if the user agent string does not contain the word "Mobile":

php
$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (!preg_match('/Mobile/i', $userAgent)) {
echo 'Not a mobile device';
} else {
echo 'Mobile device detected';
}


This code snippet checks if the user agent string does not contain the word "Mobile" and prints a message accordingly.

You can adjust the regular expression pattern and the conditional statement as needed to suit your specific requirements for detecting mobile devices based on the user agent string.

Rate this post

5 of 5 based on 2286 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us