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

 

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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