Page 1 of 1
Quick and easy - adding newlines at capital letters
Posted: Mon May 11, 2009 10:28 am
by mattpointblank
I have a text string of opening hours that I want to turn into a HTML list. Here's a typical string:
Mon-Thu 12:00-23:00, Fri-Sat 11:00-14:30, 17:00 - 02:00, Sun 12:00-23:00.
I want to end up with:
<li>Mon-Thu 12:00-23:00</li>
<li>Fri-Sat 11:00-14:30, 17:00 - 02:00</li>
<li>Sun 12:00-23:00</li>
I've tried this code but it doesn't do anything, just repeats the string.
Code: Select all
$openHours = preg_replace('/[A-Z]/', "</li>\n<li>\\0", $openHours, 1);
Basically, I'm trying to split the string at any capital letter (eg, a day of the week) and add in the <li> tags.
I'm a total regexp n00b - can anyone advise?
Cheers
Matt
Re: Quick and easy - adding newlines at capital letters
Posted: Mon May 11, 2009 10:57 am
by prometheuzz
mattpointblank wrote:...
I've tried this code but it doesn't do anything, just repeats the string.
Code: Select all
$openHours = preg_replace('/[A-Z]/', "</li>\n<li>\\0", $openHours, 1);
...
Variable interpolation isn't done through
\\0 but by
$0.
Re: Quick and easy - adding newlines at capital letters
Posted: Mon May 11, 2009 11:01 am
by John Cartwright
(Unless you are using the e modifier)
Re: Quick and easy - adding newlines at capital letters
Posted: Mon May 11, 2009 11:03 am
by mattpointblank
Okay, I changed it to $0 and removed the final limit parameter and now get:
<li>Mon-</li>
<li>Thu 12:00-23:00, </li>
<li>Fri-</li>
<li>Sat 12:00-00:00, </li>
<li>Sun 12:00-23:00.</li>
which is right, but I guess the Mon-Thu part at the start breaks it. Back to the drawing board, I need a smarter regexp...
Re: Quick and easy - adding newlines at capital letters
Posted: Mon May 11, 2009 11:13 am
by prometheuzz
mattpointblank wrote:Okay, I changed it to $0 and removed the final limit parameter and now get:
<li>Mon-</li>
<li>Thu 12:00-23:00, </li>
<li>Fri-</li>
<li>Sat 12:00-00:00, </li>
<li>Sun 12:00-23:00.</li>
which is right, but I guess the Mon-Thu part at the start breaks it. Back to the drawing board, I need a smarter regexp...
Feel free to post back if you get stuck!
Re: Quick and easy - adding newlines at capital letters
Posted: Mon May 11, 2009 11:16 am
by prometheuzz
John Cartwright wrote:(Unless you are using the e modifier)
True.
Good point.
Re: Quick and easy - adding newlines at capital letters
Posted: Mon May 11, 2009 11:19 am
by mattpointblank
Got it! I think...
Code: Select all
$openHours = preg_replace('/(, )([A-Z])/', "</li>\n<li>$2", $openHours);
From the stuff I've just been reading, as far as I can tell, that code searches for a comma, then a space, then any capital letter, and breaks those things into two interpolated variables so I can keep the capital letter in the replacement. Right? Either way, it works!
Re: Quick and easy - adding newlines at capital letters
Posted: Wed May 13, 2009 1:20 pm
by prometheuzz
mattpointblank wrote:Got it! I think...
Code: Select all
$openHours = preg_replace('/(, )([A-Z])/', "</li>\n<li>$2", $openHours);
From the stuff I've just been reading, as far as I can tell, that code searches for a comma, then a space, then any capital letter, and breaks those things into two interpolated variables so I can keep the capital letter in the replacement. Right? Either way, it works!
Yes, well done!
Note that since you don't need group 1, you can ignore it like this:
Code: Select all
$openHours = preg_replace('/, ([A-Z])/', "</li>\n<li>$1", $openHours);
Re: Quick and easy - adding newlines at capital letters
Posted: Fri May 15, 2009 3:20 am
by mattpointblank
Ahh, awesome - thanks! I've just taken my first steps into a larger world
