I have text in a MYSQL database like such:
"2/22/10 - Called Lisa today. She emailed the app.
2/8/10 - No response to previous email. Forwarded again today.
1/27/10 - Emailed Lisa Gregory to see how soon we can start the application process."

The thing I'd like to do is have the date at the beginning of each line formatted so it stands out with either bold, underline, italics.. anything. I realize not every date is the same length - it's no big deal if the dash and spaces are also formatted.

I'm just not sure where to start... preg_replace??

<?
$cleancomments=stripslashes($_POST[Comments]);
echo "<table border=\"1\" width=\"60%\">
	<tr><td>Comments</td>
	</tr>
	<tr>
		<td>".nl2br($cleancomments)."</td>
	</tr>
</table>";

Thanks in advance!

Recommended Answers

All 2 Replies

Member Avatar for diafol

Sort out your mysql table - it's daft. If you are timestamping / dating entries, there should be a datetime/date/integer field to mark this. It should not be in the main body. Once your sort this out:

Use CSS. Give the date a span tag - possibly with a classname. You can make the span tag a block level element - so it looks like a heading. You can make it bold, italic, underlined, overlined, wombling free, the wombles of Wimbledon... (sorry got carried away)... red and yellow and pink and green, purple and orange and blue. Get a handle on basic CSS and you won't need to bother the good people on the php forum.

If you insist on using the date in the body of the text in a db field, you can use substr() / substr_replace() etc to format/change your output to usable HTML. Hint - date has 8 chars - although you really should use 4 digits for the year.

Found a fix on php.net, of all places, and tweaked it :)

$line = nl2br($Comments);
    $line = preg_replace_callback(
        '|(\d{2}/\d{2}/)(\d{2})|',
        create_function(
            // single quotes are essential here,
            // or alternative escape all $ as \$
            '$matches',
            '; return "<b>".$matches[0]."</b>";'
        ),
        $line
    );
echo "<table border=\"1\" width=\"60%\">
	<tr><td>Comments</td>
	</tr>
	<tr>
		<td>$line</td>
	</tr>
</table>";

This bolds all instances of a matching date, but that's fine with me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.