Jump to content

Recommended Posts

I'm trying to dissect a string using preg_match but I'm having a hard time understanding the tutorials (and the php manual).  This is as far as I've gotten:

<?php

$message = "EASTRIVANN RES STRUCTURE FIRE AD: 4966 TURKEY SAG RD CTY: AC LOC: ATAC4 FIRE IN STRUCTURE/ALARM CO CONFIRMED W/HOMEOWNER XST: 2751 BELL ACRES XST2: 2312 PETERS MTN RD";

$typepattern = ('/^EASTRIVANN /');

preg_match($typepattern, $message, $matches);
if ($matches) {
echo $matches[0];
}else{
echo 'No matches.';
}


?>

 

My goal is to pull the following out of the string:

 

$alarmtype = "RES STRUCTURE FIRE";
$address = "4966 TURKEY SAG RD";
$county = "AC";
$location = "ATAC4 FIRE IN STRUCTURE/ALARM CO CONFIRMED W/HOMEOWNER";
$xst = "2751 BELL ACRES";
$xst2 = "2312 PETERS MTN RD";

 

I am them storing the values into a database for later retrieval.  I've gotten as far as Matching EASTRIVANN but i'm not sure how to pull out the actual text between my anchors.  Any help would greatly be appreciated :)

 

 

Thanks

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/
Share on other sites

Well i'm kind of confused on how exactly you want to match strings. What criteria does the string "RES STRUCTURE FIRE" have that qualifies it as an alarm type. what are some other examples of strings that would match for alarm type. Can you please explain the criteria you need to use for matching strings. That way we can be of much more help.

 

Also, the matched strings go into the $matched array. From the manual

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

 

Hope this helps

Thanks for the quick reply :)  I only want to match the constant text before and after the data.  Here is a few more examples:

 

EASTRIVANN TREE DOWN AD: LOUISA RD&CLOVERFIELDS FARM CTY: AC LOC: AREA OF TREE BLOCKING ROADWAY

 

EASTRIVANN PUBLIC SERVICE CALL AD: 5000 LOUISA RD CTY: AC ASSIST PD AT ACCIDENT SCENE XST: 4410 CLOVERFIELDS FARM XST2: 181 CLOVER HILL FARM

 

EASTRIVANN MVA AD: RICHMOND RD&E I64 EXIT 124 OFF CTY: AC 2 CAR UNK INJ, SLIGHT SMOKE. REAR END COLLISION, RED CRYSLER, EXPIDENTION

 

Each string that I feed into preg_match will have the same format.  I was under the impression, with the proper pattern, that I could feed the information into the $matches array and then do with it as I please.

 

I hope this helps. 

 

Jason

What you seem to not understand about preg_match is you need to create a regular expression pattern to match what you want it to match. It doesn't just magically match things if you give it to it. It doesn't know what you want it to match (and neither do I for that matter)

 

Perhaps you should start by reading a few regular expression tutorials. This was the first result on google when searching for regex tutorials: http://www.regular-expressions.info/tutorial.html

 

EDIT:

 

sorry I missread your post. You do understand that you need the proper pattern. However, I still dont really understand what the criteria for matching will be. I don't need more examples of strings.. ideally your pattern should be predictable for any string you feed into it. What is the constant text? What is the data? You need to provide more information.. I simply don't know what you mean or are referring to.

I've highlighted the data to be extracted in bold, the non bolded text will appear in every string.

 

$message = "EASTRIVANN RES STRUCTURE FIRE AD: 4966 TURKEY SAG RD CTY: AC LOC: ATAC4 FIRE IN STRUCTURE/ALARM CO CONFIRMED W/HOMEOWNER XST: 2751 BELL ACRES XST2: 2312 PETERS MTN RD";

 

So for the first match, I need to know how to tell regex to look for EASTRIVANN as the starting point and AD: as the ending poing and put RES STRUCTURE FIRE into the array.  For the second one, look for AD: and CTY: and store 4966 TURKEY SAG RD into the array.

 

 

Well you could use regex for that, but if those non-bolded items will always be in the string, just replace them with a character that you know won't be part of the string, and explode based on that character. so for example

$delimiters = array("EASTRIVANN", "CTY:", "LOC:", "XST:", "XST2:");
$replaceWith = "~";//replace the non-bolded text with a tilde character
newMessage = str_replace($delimiters, $replaceWith, $message);
//now explode on the ~ character to get all the pieces.
$array = explode($replaceWith, $newMessage);

You're a gentleman and a scholar!

 

<?php
$message = "EASTRIVANN RES STRUCTURE FIRE AD: 4966 TURKEY SAG RD CTY: AC LOC: ATAC4 FIRE IN STRUCTURE/ALARM CO CONFIRMED W/HOMEOWNER XST: 2751 BELL ACRES XST2: 2312 PETERS MTN RD";
$delimiters = array("EASTRIVANN", "AD:", "CTY:", "LOC:", "XST:", "XST2:");
$replaceWith = "~";//replace the non-bolded text with a tilde character
$newMessage = str_replace($delimiters, $replaceWith, $message);
//now explode on the ~ character to get all the pieces.
$array = explode($replaceWith, $newMessage);

echo "Call Type:<br>";
echo $array[1];
echo "<br>Address:<br>";
echo $array[2];
echo "<br>Location:<br>";
echo $array[3];
echo "<br>Cross Street 1:<br>";
echo $array[4];
echo "<br>Cross Street 2:<br>";
echo $array[5];
?>

 

Thanks so much for the help!

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.