Jump to content

php/mysql Add CR before dd/mm/yyyy in string


cjkeane

Recommended Posts

Hi everyone,

 

i am retrieving data from mysql and displaying it in a table using the below coding. In the field string of $row['History'] there is a date followed by a description. This is how the data was inserted by the client previously. What I'd like to do is insert a CR just before the dd/mm/yyyy of each entry within the string.

 

<?php
        $ID = $_GET['ID'];
        $result = mysql_query("SELECT ID, History FROM historytable WHERE ID='$ID'") or die(mysql_error());  
        echo "<table border='0' cellpadding='1'>";
        echo "<tr>";
        while($row = mysql_fetch_array( $result )) {
                echo "<tr>";
	echo '<td>' . preg_replace('/\d\d\//', '<br>$0', $row['History']). '</td>';
                echo "</tr>"; 
        } 
        echo "</table>";
?>

 

currently it's being displayed like so:

 

01/

08/2007 this is the first line of text

01/

04/2007 this is the second line of text

01/

04/2007 this is the third line of text

 

but i need it to be displayed like this.

 

01/08/2007 this is the first line of text

01/04/2007 this is the second line of text

01/04/2007 this is the third line of text

 

Any assistance would be appreciated. Thanks.

Link to comment
Share on other sites

Are you sure there aren't already linebreaks in the data? I.e. does it look like this in the source of the HTML (if you don't add any formatting to it):

01/08/2007 this is the first line of text
01/04/2007 this is the second line of text
01/04/2007 this is the third line of text

 

If so, then all you need to do is output the data using nl2br() such as

while($row = mysql_fetch_array( $result ))
{
    $output = nl2br($row['History']);
    echo "<tr><td>{$output}</td></tr>\n";
} 

Link to comment
Share on other sites

I'm sure there aren't any line breaks. If I display the data, it comes out word wrapped, like so:

 

01/08/2007 this is the first line of text 01/04/2007 this is the second line of text 01/04/2007 this is the third line of text

 

I need to some how insert a CR before each date so it lines up.

Link to comment
Share on other sites

Yes, that's exactly what the field contains. It was actually imported from another db into mysql in that fashion, so the entire field contains that entire string. I know it would have been better to have the date in a completely separate field, but there was nothing I could do about how the data was previosly imported. All I want to do is line up the data so that each date is underneath each other. I hope that makes sense.

 

Link to comment
Share on other sites

No need for a regex pattern to do what you described.

 

SELECT ID, CONCAT('<br>', History) FROM . . . 

 

I have tried your suggestion and it still comes out as:

 

01/08/2007 this is the first line of text 01/04/2007 this is the second line of text 01/04/2007 this is the third line of text

Link to comment
Share on other sites

Yeah, I thought each of those date/string pairs was in its own record when I posted that.

 

This pattern seems to work when I test it, but I'm not sure it's the best thing to use. Maybe someone else can chime in and say whether it's OK or not; regex isn't one of my strongest areas (not by a long shot).

 

preg_replace('/[^a-zA-Z]+?\s/', '<br>$0', $row['History']);

Link to comment
Share on other sites

Yeah, thoughe each of those date/string pairs was in its own record when I posted that.

 

This pattern seems to work when I test it, but I'm not sure it's the best thing to use. Maybe someone else can chime in and say whether it's OK or not; regex isn't one of my strongest areas (not by a long shot).

 

preg_replace('/[^a-zA-Z]+?\s/', '<br>$0', $row['History']);

 

it's getting closer, but now it displays as:

 

01/08/2007

this is the first line of text

01/04/2007

this is the second line of text

01/04/2007

this is the third line of text

Link to comment
Share on other sites

Again, just change the damn RegEx in the OP to match the entire date, and not just two digits followed by a forward slash.

 

It's easy enough to do, but if OPs coded the original snippet, he should be able to do it no problems :)

Link to comment
Share on other sites

Again, just change the damn RegEx in the OP to match the entire date, and not just two digits followed by a forward slash.

 

It's easy enough to do, but if OPs coded the original snippet, he should be able to do it no problems :)

 

i resolved it by using: preg_replace('/(\d\d)\/(\d\d)\/(\d\d\d\d)/', '<br>$0', $row['History'])

 

thanks for the tip!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.