I would like to replace the value of my variable $os
with the preg_replace
function, where I have an array of multiple regex patterns
to be searched from and and array of multiple replacements to replace with. If $pattern_os[0]
matches $os
, it shall also be replaced with the corresponding $replace_os[0]
.
Here is my code:
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$pattern_os = array('/.*Mac.*/','/.*Windows.*/');
$replace_os = array('Mac','Windows');
$os = $useragent;
$no_os = "No operating system was found. <br />";
for($i=0;$i<2;$i++){
preg_replace($pattern_os[$i], $replace_os[$i], $os);
}
echo $os;
?>
This is the output I get:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14
Of course I'd like to have an output like this:
Mac
It'd be nice if someone could help me with this problem.
I'm new here, frustrated after failing so many times and this is my first question, so telling me what to improve on asking or where to search (although I think I spent enough time googling) might also help a bit.
Thanks