moola Posted December 16, 2006 Share Posted December 16, 2006 Test Location: United States - South Carolina - Greenville - Bellsouth.net InI need to grab the following Greenville and South Carolina from the exact format of the string above.. I am using the fget function to get the string from a website into an $buffer $buffer .= fgets($fd, 4096);Basically i need to extract South Carolina and put it into $state And Greenville and put it into $cityother variables would be cool but not necessary. This may be a bit of an easy question but I really got no time to master regex's right not I would love to learn it soon. I'm new to php still learning some other stuff before i get to everyone favorite lol Quote Link to comment Share on other sites More sharing options...
bljepp69 Posted December 16, 2006 Share Posted December 16, 2006 If the string is always in that format:[code]<?php $buffer = "United States - South Carolina - Greenville - Bellsouth.net In"; preg_match_all('/(.*)\s-\s(.*)\s-\s(.*)\s-\s(.*)/',$buffer,$matches,PREG_PATTERN_ORDER); $state = $matches[2][0]; $city = $matches[3][0]; //use this code below to see the values and structure of the $matches array. You could set other variables using the same format above. echo "MATCHES:<pre>".print_r($matches,TRUE)."</pre>"; ?>[/code] Quote Link to comment Share on other sites More sharing options...
Nicklas Posted December 16, 2006 Share Posted December 16, 2006 Use explode()ex[hr][code=php:0]$str = "Test Location: United States - South Carolina - Greenville - Bellsouth.net In";list( , $state, $city) = explode(' - ', $str);[/code][hr]Now the state is stored in [b]$state[/b], and the city in [b]$city[/b] ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.