Jump to content

Help me pleeeeease! My head is sore from banging it into the wall! lol


FiremanSam

Recommended Posts

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...

 

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 

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));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.