I have a list of Mobile devices that I'm using to display content correctly. The depreciated function looks like this:
function detectPDA($query){
$browserAgent = $_SERVER['HTTP_USER_AGENT'];
$userAgents = $this->getBrowserAgentsToDetect(); // comma separated list of devices
foreach ( $userAgents as $userAgent ) {
if(eregi($userAgent,$browserAgent)){
if(eregi("iphone",$browserAgent) || eregi("ipod",$browserAgent) ){
$this->iphone = true;
}else{
$this->pda = true;
}
}
}
}
What is the correct way to replace the eregi functions?
If all the pattern strings (
$userAgentandiphone) can be trusted not to contain special regex chars (()[]!|.^${}?*+), then you just surround theeregiregex with slashes (/) and add aniafter the last slash (which means "case insensitive").So:
However, are you just trying to match
$userAgentas-is within$browserAgent? For example, if a particular$userAgentwasfoo.bar, would you want the.to match a literal period, or would you want to interpret it in its regex sense ("match any character")?If the former, I'd suggest you forgo regex entirely and use
stripos($haystack,$needle), which searches for the string$needlein$haystack(case-insensitive). Then you don't need to worry about (say) an asterisk in$userAgentbeing interpreted in the regex sense instead of the literal sense.If you do use
striposdon't forget it can return a0which would evaluate tofalse, so you need to use=== falseor!== false(see the documentation I linked).