For PHP
I have a date I want line wrapped.
I have $date = '2008-09-28 9:19 pm'; I need the first space replaced with a br to become
2008-09-28<br>9:19 pm
If it wasn't for that second space before PM, I would just str_replace() it.
For PHP
I have a date I want line wrapped.
I have $date = '2008-09-28 9:19 pm'; I need the first space replaced with a br to become
2008-09-28<br>9:19 pm
If it wasn't for that second space before PM, I would just str_replace() it.
s/ /<br\/>/
should do it. Though PHP regex might be greedy and replace all spaces.
I support Ben Hoffstein's PHP solution. Where possible, avoid regex as it nearly always has unintended side effects.
$date = '2008-09-28 9:19 pm';
$result = preg_replace('/(\d{4}-\d{2}-\d{2}) (.*)/', '$1<br>$2', $date);
If you want a regex to match the pattern and return both parts, you can use the following. However, considering all that you're doing is replacing only 1 space, try the str_replace_once
that I'll suggest after the regex. Regex is for complicated parsing instead of a wasteful use like replacing one space (no offense intended).
Please note that the following is browser-code, so don't attempt to use it verbatim. Just in case of a typo.
$regex = '/([\d]{4}-[\d]{2}-[\d]{2}) ([\d]{1,2}:[\d]{2} (am|pm))/';
$match = preg_match($regex, '2008-09-28 9:19 pm');
print $match[1]; // Returns 2008-09-28
print $match[2]; // Returns 9:19 pm
// Or replace:
preg_replace($regex, '$1<br>$2', $date);
I suggest the following, much faster mechanism. See this post for the function str_replace_once()
.
str_replace_once(' ', '<br>', $date);
The other way to do this, which is faster but a bit more code, is
$date = substr($date, 0, 10) . '<br>' . substr($date, 11);