cjkeane Posted July 3, 2012 Share Posted July 3, 2012 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2012 Share Posted July 3, 2012 No need for a regex pattern to do what you described. SELECT ID, CONCAT('<br>', History) FROM . . . Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 3, 2012 Share Posted July 3, 2012 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"; } Quote Link to comment Share on other sites More sharing options...
cjkeane Posted July 3, 2012 Author Share Posted July 3, 2012 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2012 Share Posted July 3, 2012 Are you saying that all of 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 would be stored in the same field? Quote Link to comment Share on other sites More sharing options...
cjkeane Posted July 3, 2012 Author Share Posted July 3, 2012 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. Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 Check for more than just \d\d/... like the entire date pattern Quote Link to comment Share on other sites More sharing options...
cjkeane Posted July 3, 2012 Author Share Posted July 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2012 Share Posted July 3, 2012 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']); Quote Link to comment Share on other sites More sharing options...
cjkeane Posted July 3, 2012 Author Share Posted July 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
xyph Posted July 3, 2012 Share Posted July 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
cjkeane Posted July 3, 2012 Author Share Posted July 3, 2012 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.