Quick and easy - adding newlines at capital letters

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Quick and easy - adding newlines at capital letters

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Quick and easy - adding newlines at capital letters

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Quick and easy - adding newlines at capital letters

Post by John Cartwright »

(Unless you are using the e modifier)
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Quick and easy - adding newlines at capital letters

Post 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...
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Quick and easy - adding newlines at capital letters

Post 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!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Quick and easy - adding newlines at capital letters

Post by prometheuzz »

John Cartwright wrote:(Unless you are using the e modifier)
True.
Good point.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Quick and easy - adding newlines at capital letters

Post 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!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Quick and easy - adding newlines at capital letters

Post 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);
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Quick and easy - adding newlines at capital letters

Post by mattpointblank »

Ahh, awesome - thanks! I've just taken my first steps into a larger world :P
Post Reply