jtetterton Posted May 28, 2011 Share Posted May 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/ Share on other sites More sharing options...
mikesta707 Posted May 28, 2011 Share Posted May 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221685 Share on other sites More sharing options...
jtetterton Posted May 28, 2011 Author Share Posted May 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221690 Share on other sites More sharing options...
mikesta707 Posted May 28, 2011 Share Posted May 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221692 Share on other sites More sharing options...
jtetterton Posted May 28, 2011 Author Share Posted May 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221700 Share on other sites More sharing options...
jtetterton Posted May 28, 2011 Author Share Posted May 28, 2011 I just noticed there was a REGEX forum, if a moderator could move this, that would be appreciated (I hate it when people double post in multiple forums). Jason Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221704 Share on other sites More sharing options...
mikesta707 Posted May 28, 2011 Share Posted May 28, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221706 Share on other sites More sharing options...
jtetterton Posted May 28, 2011 Author Share Posted May 28, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221709 Share on other sites More sharing options...
mikesta707 Posted May 28, 2011 Share Posted May 28, 2011 No problem. If your topic is solved, you can click the topic solved button at the bottom of the thread Quote Link to comment https://forums.phpfreaks.com/topic/237733-assistance-with-preg_match/#findComment-1221711 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.