FiremanSam Posted March 9, 2012 Share Posted March 9, 2012 Hi, I am trying to extract a address from a txt file (Emergency Pager Messages) and I've got to the point where I strip everything not needed in the string but I am left with this: BURNOFF THREATENING 33 SOMEPLACE RD MONTROSE My question is this, how do I strip everything BEFORE (which could be numerous different words) the number leaving just: 33 SOMEPLACE RD MONTROSE And second question how can I strip everything after the number (including the number itself) leaving just: BURNOFF THREATENING There can be a thousand different words before the number describing the job and yer as I said I need to strip them to leave the address but also need to strip the address leaving the description only... I hope that makes sense and I'd REALLY appreciate any pointers in the right direction... Quote Link to comment Share on other sites More sharing options...
xyph Posted March 9, 2012 Share Posted March 9, 2012 So you want to split the string immediately before the first number? Quote Link to comment Share on other sites More sharing options...
FiremanSam Posted March 9, 2012 Author Share Posted March 9, 2012 Yep. Sorry if I confusing! It's 3.30am here and I confuse myself too. lol Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2012 Share Posted March 9, 2012 Help me pleeeeease! My head is sore from banging it into the wall! lol Well, then stop banging your head into the wall then. Problem solved. Here is one solution: $input = "BURNOFF THREATENING 33 SOMEPLACE RD MONTROSE"; preg_match("#\d.*#", $input, $addressMatch); $address = $addressMatch[0]; echo "Address: $address"; //Output: Address: 33 SOMEPLACE RD MONTROSE preg_match("#^[^\d]+#", $input, $beforeMatch); $beforeText = trim($beforeMatch[0]); echo "Text before address: $beforeText"; //Output: Text before address: BURNOFF THREATENING Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 9, 2012 Share Posted March 9, 2012 This should be slightly more efficient since only one regular expression is needed $input = "BURNOFF THREATENING 33 SOMEPLACE RD MONTROSE"; preg_match("#\d#", $input, $matches, PREG_OFFSET_CAPTURE); $cutPosition = $matches[0][1]; $beforeText = trim(substr($input, 0, $cutPosition)); $address = trim(substr($input, $cutPosition)); 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.