Reputation: 14835
Here is the string I have from a server:
70.90.184.9 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1
The data I need pulled out should be like this:
$ip = "70.90.184.9"
$os = "Intel Mac OS X 10_8_4"
Is there a RegEx to get the IP out easily in PHP? And what about the OS?
Thanks!
Upvotes: 0
Views: 71
Reputation: 89557
This pattern do the job for your example, but i am not sure that it will be the same for an another string:
^([\d.]++)[^(]++\(\S++\s([^)]++)
If you want a more general pattern, please update your post with some possible examples which can be useful to determine a more global syntaxe. However, regex alternatives as Sverri suggests is perhaps a good way.
Upvotes: 0
Reputation: 13283
Use the PHPBrowsCap project. It is a stand-alone class that tries to get around the limitations of the get_browser()
function.
Parsing user agent strings is notoriously difficult and unreliable. The aforementioned project is probably the best way of getting usable user agent information. That being said, please do not use user agent strings to put up "This website only supports browser X" messages -- people will hate you for it.
Upvotes: 2
Reputation: 158010
Use the function get_browser()
for this. It's a hidden treasure ;) Call:
$str = '70.90.184.9 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1';
var_dump(get_browser($str));
Upvotes: 2