CodeIgniter Forums

Full Version: regex help needed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've been trying to figure this out for awhile, so it must be time to ask for help. How can I get the results I want?

Code:
$subject = '\n   MC - 5-10 OVER IN A CHURCH ZONE    - 12/08/15\n    INF - 1-5 OVER IN A SCHOOL ZONE    - 12/14/15\n';

$matches = preg_split('/ ([MF].|INF|CAP) \-(.*) \- /',$subject, NULL); // also tried: preg_match, preg_match_all
// desired result: 2 string values placed into array
// 1. MC - 5-10 OVER IN A CHURCH ZONE    -
// 2. INF - 1-5 OVER IN A SCHOOL ZONE    -
(05-09-2016, 09:54 AM)cupboy Wrote: [ -> ]I've been trying to figure this out for awhile, so it must be time to ask for help. How can I get the results I want?

Code:
$subject = '\n   MC - 5-10 OVER IN A CHURCH ZONE    - 12/08/15\n    INF - 1-5 OVER IN A SCHOOL ZONE    - 12/14/15\n';

$matches = preg_split('/ ([MF].|INF|CAP) \-(.*) \- /',$subject, NULL); // also tried: preg_match, preg_match_all
// desired result: 2 string values placed into array
// 1. MC - 5-10 OVER IN A CHURCH ZONE    -
// 2. INF - 1-5 OVER IN A SCHOOL ZONE    -

is the information you're going to be getting always in this format?
In regex, to recognize a space character, use \s
To get a match for multiple space characters, use \s+
Regular Expression online tester RegExr.

Online RegX Tester
I figured out how to get the results. It's a two step process though. Probably is a better way. For the time being the 2nd process is a little bit like this:
Code:
 $pattern = '/([MF].|INF|CAP) \- /';
 $subject = $matches[0][0];
 $splits = preg_split($pattern,$subject,NULL,PREG_SPLIT_DELIM_CAPTURE);
Final result looks like this:
Array ( [0] => [1] => MA [2] => POSSESSION OR USE OF A CONTROLLED SUBSTANCE - 10/23/14 [3] => MA [4] => USE OR POSSESSION OF DRUG PARAPHERNALIA - )

.. everything is split up the way I want it (the dates I don't need but I can use a third process to dump those)