Hi all,
I need to split one string into three.
It's variable name is $itemname
The string looks (exactly) like this:
Compose Yourself , Sunday 21st August 2011. 3.45pm - 6.45pm
or this:
Day One Digital , Saturday 20th August 2011. 11.45am - 2.45pm
or very similar to the above example - i.e. title followed by a space then a comma and a space, a date (long form) followed by a fullstop (period) and then a time.
I need to split this string into three strings (title, date and time) each with a unique variable name. ($course, $date and $time).
As an experienced Flasher (AS2 and 3) I know how to split these on the first space or comma and then on the first period but having tried a few different ways in php
I'm having no joy.
All and any advice / help / snippets much appreciated.
Best wishes
Monty
Split one string into three ?
Moderator: General Moderators
Re: Split one string into three ?
While I'm looking at this, was are the chances of the Course Title containing a comma in it, and if so, will it be like:
My Course, The Best There Is , Friday 2nd August 2011. 3.12pm - 4.12pm
Will the date always be that format, and will the time listing always be that format, starting and ending time.
My Course, The Best There Is , Friday 2nd August 2011. 3.12pm - 4.12pm
Will the date always be that format, and will the time listing always be that format, starting and ending time.
Re: Split one string into three ?
Here is something that will work:
Code: Select all
if (preg_match('/^(.+) , (((Sun|Mon|Tues|Wed|Thur|Fri|Sat).+) ([0-9]{1,2})[dhnrst]{2} ([^ ]+) ([0-9]{4})). (.+)$/i', $itemname, $regs)) {
$course = $regs[1];
$date = $regs[2]; // Saturday 20th August 2011
$time = $regs[8]; // 11.45am - 2.45pm
// $regs[0] = Full matched string (Basically the original string)
// $regs[3] = Full day of week (Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday)
// $regs[4] = Short day of week (Sun/Mon/Tues/Wed/Thurs/Fri/Sat)
// $regs[5] = Day of Month (20)
// $regs[6] = Month Name (Full name)
// $regs[7] = Year
} else {
// NO MATCH
}
Re: Split one string into three ?
That's great - works perfectly - thanks.
Care to talk me through the first line so I can understand exactly what's going on ?
Best wishes
Monty
Care to talk me through the first line so I can understand exactly what's going on ?
Best wishes
Monty
Re: Split one string into three ?
The first line tries to match the regular expression (everything in the single quotes) against $itemname, and if it does find it, places the parts that match captured sections which are wrapped in parenthesis into the variable $regs as an array. (with [0] being the whole section that matches.
Here is breakdown on the regex itself:
The /'s wrap the expression, with options listed after the closing one. In this case, it uses i to indicate to match case insensitive
the ^ indicated it needs to match the beginning of the string
the (.+) says to match any character at least one, up to unlimited times, and returns it as $regs[1]
then there are the literal character matches to a space, comma, space
next we start a capture for the entire date section
then we start a capture for the entire day of week.
and then we do a match for any of the following strings, which will get us an abbreviated day of week (wasn't needed, but added in case you want it)
so then we have to handle the remaining characters for the day of the week, so I did .+ to capture the rest.
We are done capturing the day of the week, so now look for an exact character of a space
next it will capture the day of the month, which consists of a class of characters, 0-9, being 1-2 characters in length
Since yours has st,nd,rd,th suffix on days of the month, we now match a class of characters of each letter that makes those up, exactly 2 characters
Now we look for an exact character of a space
Then ([^ ]+) captures characters in a class of NOT being (^) a space that is at least one character long
Now we look for an exact character of a space
We capture a class of characters that are 0-9, exactly 4 characters long
Actually here is a mistake, but still works. In my code i put a period, which will match any character, so it will match the period that I meant to match. I should have escaped the . so it would be treated as the actual character period.
Now we look for an exact character of a space
then we finally capture all remaining characters in the string until it hits the end of the string.
The $ is what indicates it needs to match the end of the string.
Again as mentioned at the start, we have the closing / to wrap the expression, followed by the i to not need to match case.
Pretty much it. A great program (and what I used to make sure I wrote it right), is RegexBuddy, I paste in your examples, and then it highlights it all as I biuld it and lists out all the captures. That time that program has saved me was well worth it over the years.
Here is breakdown on the regex itself:
The /'s wrap the expression, with options listed after the closing one. In this case, it uses i to indicate to match case insensitive
the ^ indicated it needs to match the beginning of the string
the (.+) says to match any character at least one, up to unlimited times, and returns it as $regs[1]
then there are the literal character matches to a space, comma, space
next we start a capture for the entire date section
then we start a capture for the entire day of week.
and then we do a match for any of the following strings, which will get us an abbreviated day of week (wasn't needed, but added in case you want it)
so then we have to handle the remaining characters for the day of the week, so I did .+ to capture the rest.
We are done capturing the day of the week, so now look for an exact character of a space
next it will capture the day of the month, which consists of a class of characters, 0-9, being 1-2 characters in length
Since yours has st,nd,rd,th suffix on days of the month, we now match a class of characters of each letter that makes those up, exactly 2 characters
Now we look for an exact character of a space
Then ([^ ]+) captures characters in a class of NOT being (^) a space that is at least one character long
Now we look for an exact character of a space
We capture a class of characters that are 0-9, exactly 4 characters long
Actually here is a mistake, but still works. In my code i put a period, which will match any character, so it will match the period that I meant to match. I should have escaped the . so it would be treated as the actual character period.
Now we look for an exact character of a space
then we finally capture all remaining characters in the string until it hits the end of the string.
The $ is what indicates it needs to match the end of the string.
Again as mentioned at the start, we have the closing / to wrap the expression, followed by the i to not need to match case.
Pretty much it. A great program (and what I used to make sure I wrote it right), is RegexBuddy, I paste in your examples, and then it highlights it all as I biuld it and lists out all the captures. That time that program has saved me was well worth it over the years.