[PHP] Re: parsing useragent string without get_browser
The problem is I haven't seen any examples of this in php. The best I have come up with is the following. I know the code is pants, but it works. I am sure people out there can think of a better way of doing it- $ua = $logInfo[useragent]; if ( ereg(Firefox/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(MSIE [0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(Bloglines/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(Amfibibot/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(msnbot/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(Googlebot/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(Safari/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(Konqueror/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(Netscape/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if ( ereg(Thunderbird/[0-9]{1,2}[\.]{0,1}[0-9]{0,4}[\.]{0,1}[0-9]{0,4}, $ua, $array)) {$browser = $array[0];} if (strpos($ua, Web RSS Reader)!== FALSE) {$browser = Web RSS Reader;} if (strpos($ua, BDFetch)!== FALSE) {$browser = BDFetch;} if (strpos($ua, www.almaden.ibm.com/cs/crawler)!== FALSE) {$browser = Web Fountain;} if (strpos($ua, sohu-search)!== FALSE) {$browser = Sohu Search;} if (strpos($ua, Yahoo! Slurp)!== FALSE) {$browser = Yahoo! Slurp;} if (strpos($ua, Windows NT 5.1)) {$platform = Windows XP;} elseif (strpos($ua, Windows NT 5.0)) {$platform = Windows 2000;} elseif (strpos($ua, Windows 98) OR strpos($ua, Win98)) {$platform = Windows 98;} elseif (strpos($ua, Windows 95) OR strpos($ua, Win95)) {$platform = Windows 95;} elseif (strpos($ua, Win16) OR strpos($ua, Windows 3.1)) {$platform = Windows 3.1;} elseif (strpos($ua, Mac OS X)) {$platform = Mac OSX;} elseif (strpos($ua, Linux)) {$platform = Linux;} -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: parsing a string
Dan McCullough wrote: Hey everyone Having a bit of trouble with something. I have a string which has known patterns. $stringCampusBob (Williams)~\toms more crap)~\blah blah blah)~\; What I am looking for is Location which is Campus Location Name which is Bob (Williams) $x = explode('~', $string); list($location, $name) = explode('', $x[0]); Make sure that for each \ in the string, you use \\ (that's why there are 10 in my example. If you have 5 \ you need 5 \\) Greg -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: parsing a string
Not sure what you want exactly, but here's a way using regexps to retrieve the strings seperatly: ?php $string = 'CampusBob (Williams)~\toms more crap)~\blah blah blah)~\'; preg_match('#([^|]*)[|]+([^|]*)~[\]+([^\]*)~[\]+([^\]*)~[\]+#Ui', $string, $res); $campus = $res[1]; $bob = $res[2]; $crap = $res[3]; $blah = $res[4]; ? Now, if you wanted the positions... then something like this would work: ?php $string = 'CampusBob (Williams)~\toms more crap)~\blah blah blah)~\'; preg_match('#([^|]*)[|]+([^|]*)~[\]+([^\]*)~[\]+([^\]*)~[\]+#Ui', $string, $res); $campus = strpos($string, $res[1]); $bob = strpos($string, $res[2]); $crap = strpos($string, $res[3]); $blah = strpos($string, $res[4]); ? Dan McCullough wrote: Hey everyone Having a bit of trouble with something. I have a string which has known patterns. $stringCampusBob (Williams)~\toms more crap)~\blah blah blah)~\; What I am looking for is Location which is Campus Location Name which is Bob (Williams) Help? - Do you Yahoo!? vote.yahoo.com - Register online to vote today! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: parsing a string
I knew I shouldnt have abreviated the string. here is the string sorry I kinda flubbed on the last string LocationCampus~\\n-\nNameBob Williams~\\n-\nAddress123 Main St~\\n-\n... the ... is a very long list. how does this change the preg_match M. Sokolewicz [EMAIL PROTECTED] wrote: Not sure what you want exactly, but here's a way using regexps to retrieve the strings seperatly: $string = 'CampusBob (Williams)~\toms more crap)~\blah blah blah)~\'; preg_match('#([^|]*)[|]+([^|]*)~[\]+([^\]*)~[\]+([^\]*)~[\]+#Ui', $string, $res); $campus = $res[1]; $bob = $res[2]; $crap = $res[3]; $blah = $res[4]; ? Now, if you wanted the positions... then something like this would work: $string = 'CampusBob (Williams)~\toms more crap)~\blah blah blah)~\'; preg_match('#([^|]*)[|]+([^|]*)~[\]+([^\]*)~[\]+([^\]*)~[\]+#Ui', $string, $res); $campus = strpos($string, $res[1]); $bob = strpos($string, $res[2]); $crap = strpos($string, $res[3]); $blah = strpos($string, $res[4]); ? Dan McCullough wrote: Hey everyone Having a bit of trouble with something. I have a string which has known patterns. $stringCampusBob (Williams)~\toms more crap)~\blah blah blah)~\; What I am looking for is Location which is Campus Location Name which is Bob (Williams) Help? - Do you Yahoo!? vote.yahoo.com - Register online to vote today! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Theres no such thing as a problem unless the servers are on fire! - Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses.
Re: [PHP] Re: parsing a string
I tried the print_r on $res. The preg_match does the first set fine. So I get: Campus Bob (Williams) the second one starts - Address123 Main St - CityOxford and so on [EMAIL PROTECTED] wrote: preg_match('#([^|]*)[|]+([^|]*)~[\\]+(([^\\]*)~[\\]+)+#Ui', $string, $res); use print_r on the $res to check for the results (I'm not 100% sure if they'll come out in $res[2], $res[3], $res[4], etc. or in $res[4][0], $res[4][1], etc. I knew I shouldnt have abreviated the string. here is the string sorry I kinda flubbed on the last string LocationCampus~\\n-\nNameBob Williams~\\n-\nAddress123 Main St~\\n-\n... the ... is a very long list. how does this change the preg_match M. Sokolewicz wrote: Not sure what you want exactly, but here's a way using regexps to retrieve the strings seperatly: $string = 'CampusBob (Williams)~\toms more crap)~\blah blah blah)~\'; preg_match('#([^|]*)[|]+([^|]*)~[\]+([^\]*)~[\]+([^\]*)~[\]+#Ui', $string, $res); $campus = $res[1]; $bob = $res[2]; $crap = $res[3]; $blah = $res[4]; ? Now, if you wanted the positions... then something like this would work: $string = 'CampusBob (Williams)~\toms more crap)~\blah blah blah)~\'; preg_match('#([^|]*)[|]+([^|]*)~[\]+([^\]*)~[\]+([^\]*)~[\]+#Ui', $string, $res); $campus = strpos($string, $res[1]); $bob = strpos($string, $res[2]); $crap = strpos($string, $res[3]); $blah = strpos($string, $res[4]); ? Dan McCullough wrote: Hey everyone Having a bit of trouble with something. I have a string which has known patterns. $stringCampusBob (Williams)~\toms more crap)~\blah blah blah)~\; What I am looking for is Location which is Campus Location Name which is Bob (Williams) Help? - Do you Yahoo!? vote.yahoo.com - Register online to vote today! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Theres no such thing as a problem unless the servers are on fire! - Do you Yahoo!? Yahoo! Mail - Helps protect you from nasty viruses. Theres no such thing as a problem unless the servers are on fire! - Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone.
[PHP] Re: parsing a string
Yz James [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hey John, something like this might work: ? $string = This is a string with an embedded image [bird.gif,this is a bird]; $string = preg_replace(/\[(.*?\.)(gif|jpg),(.*?)\]/i, img src=\\\1\\2\ alt=\\\3\, $string); echo $string; ? Great!. I love it when I see stuff like this written by someone who knows regex better than I. The ability to insert the pieces that are recognized/extracted via \1, \2 is pretty cool. Thanks. -- John A. Grant * I speak only for myself * (remove 'z' to reply) Radiation Geophysics, Geological Survey of Canada, Ottawa If you followup, please do NOT e-mail me a copy: I will read it here -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: parsing a string
Hey John, something like this might work: ? $string = This is a string with an embedded image [bird.gif,this is a bird]; $string = preg_replace(/\[(.*?\.)(gif|jpg),(.*?)\]/i, img src=\\\1\\2\ alt=\\\3\, $string); echo $string; ? James John A. Grant [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I'm reading some HTML text from a file and echoing it to stdout. The HTML text contains IMG but I would rather have the server do the work of looking up the image size. I know how to lookup the image size with getimagesize(). My problem is in coming up with a good format for embedding a reference to the image in the text and then writing the code to parse it. So instead of this: here is some text img src=bird.gif width=100 height=20 alt=this is a bird and here is more text and another image img src=plane.gif width=123 height=23 alt=this is a plane and more text I would like to have something like this: here is some text [bird.gif,this is a bird] and here is more text and another image [plane.gif, this is a plane] and more text Crossing line boundaries is not an issue - each text string is complete. I need to be able to dump out the string until I see a reference to an image, then extract the name and alt text, handle it (by emitting IMG) and continue to echo text from the string until I encounter another image reference. My problem is in coming up with a syntax for this and then to write the code to extract the information. In the above example, I'm using the syntax: [filename,text] but it's conceivable that the HTML text might also contain [some plain text not related to images] so I thought about some of these: {filename,alt text} - not good, text might contain {plain text] @filename, alt text@ img(filename,alt text) Using the same @ delimiter at each end might make it easier to use explode() to split the text. But perhaps img(filename,text) is more elegant, but it might need more skills than I have in using regex to recognize it and extract it. Also I need to figure out how to extract and echo the plain text around it. Any ideas are appreciated. Thanks. -- John A. Grant * I speak only for myself * (remove 'z' to reply) Radiation Geophysics, Geological Survey of Canada, Ottawa If you followup, please do NOT e-mail me a copy: I will read it here -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]