surreal5335 Posted April 25, 2010 Share Posted April 25, 2010 I am working on creating a post that will replace all carriage returns with a <br /> tag so the format the user entered will be preserved. here is the code I am using for my create_post() and the str_replace() for replacing carraige returns. $body = str_replace("\r","<br />",$_POST['txt']); /************************************************************************/ function create_post($params) { $connection = db_connect(); $body; $query = sprintf("INSERT INTO comtable SET title = '%s', name = '%s', txt = '%s', email = '%s', time = NOW(), user_id = '%s'", mysql_real_escape_string($params['title']), mysql_real_escape_string($params['name']), mysql_real_escape_string($params['txt']), mysql_real_escape_string($params['email']), mysql_real_escape_string($params['time']), mysql_real_escape_string($params['user_id'])); $result = mysql_query($query); if (!$result) return false; else return true; } here is my post page for displaying the database results: <?php include_once('model/post.php'); ?> <table width="400" border="1"> <tr> <td> <h2><?php echo $post['title']; ?></h2> </td> </tr> <tr> <td> name: <?php echo '<a href="'.$post['email'].'">'.$post['name'].'</a>'; ?> <!--date created: <?php echo $hr.':'.$min.' '.$timetype.' '. $mo.'/'.$da.'/'.$yr; ?>--> post id: <?php echo $post['post_id']; ?> </td> </tr> <tr> <td> comment: <br/> <?php echo $body; ?> </td> </tr> </table> The file included is the one containing the create_post() and $body. It seems to me right now I am not getting any database data in $body, just the function to replace the returns with <br />. The form that is getting this data and the returns from the user is below: <textarea cols="40" name="post[txt]" rows="5"></textarea> obviously there is more, but this is the relvent part. The method of the form is set to "post" I appreciate the help Quote Link to comment https://forums.phpfreaks.com/topic/199648-trouble-replacing-carriage-returns-with-br-tag/ Share on other sites More sharing options...
Mchl Posted April 25, 2010 Share Posted April 25, 2010 Don't do this when saving data. Just use nl2br when displaying Quote Link to comment https://forums.phpfreaks.com/topic/199648-trouble-replacing-carriage-returns-with-br-tag/#findComment-1047895 Share on other sites More sharing options...
surreal5335 Posted April 26, 2010 Author Share Posted April 26, 2010 Hey, thanks a lot, works perfect. Quote Link to comment https://forums.phpfreaks.com/topic/199648-trouble-replacing-carriage-returns-with-br-tag/#findComment-1048372 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.