Zombies Posted January 26, 2009 Share Posted January 26, 2009 Need Help with nl2br Its placed in a file showing text and as far as I know not in a file editing. It displays r\n\r\n instead of line breaks and anywords with a ' in it places about a dozen / Any ideas guys? I will look through the threads The code = case "textarea": echo "<tr>"; echo "<td valign='top' colspan=2><div><strong>".$field['Title']."</strong></div>"; echo "<p>"; echo nl2br($listing[$field['Title']]); echo "</p>"; echo "</td>"; break; Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/ Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 It sounds like you are adding too many slashes when inserting the data into your database. You should not see the \r\n characters period... Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746206 Share on other sites More sharing options...
Sudden Posted January 26, 2009 Share Posted January 26, 2009 I think you're looking for stripslashes(string) ; This removes all "/" added in by php where ' are found. Ex. <?php $str = "Is your name O\'reilly?"; // Outputs: Is your name O'reilly? echo stripslashes($str); ?> Hope this helped, but I'm not sure about the r\n\r\n part. Hope someone else out there knows Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746239 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 Hi Thanks for your replies. I know nothing about PHP coding unfortunately so if anyone could place the code as i need it like above i'd appreciate it. Heres what happens instead of 1 2 3 456 I get 1\r\n2\r\n\r\n3\r\n\r\n\r\n456 Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746432 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 putting in stripslashes gets rid of \ echo stripslashes(nl2br($listing[$field['Title']])); now shows rnrn just need to get rid of rn striprnrn ? Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746842 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 How is data being entered into your DB? To fix the problem we need to see that, as it will continue to do it unless you fix it at it's source. There is a half-assed way around it with str_replace, but I would recommend fixing the actual problem at it's source. Post the code where it get's entered into the DB if you want further assistance to fix the actual issue. Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746852 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 Thanks in blue I imagine is the section where its entered, hope this helps while( $field = mysql_fetch_assoc($result) ){ if($field['Type'] != "Mapping"){ switch($field['Type']){ case "text": echo "<tr>"; echo "<td valign='top' width='20%'><strong>".$field['Title']."</strong></td>"; echo "<td >"; if( ($field['MaxChars']*1) > 0 ) $maxlen = $field['MaxChars']; echo "<input type='text' maxlength='$maxlen' name='".$field['Title']."' value='".$listing[$field['Title']]."' validate='".$field['Validate']."' required='".$field['Required']."' message='".$field['Message']."'>"; break; case "textarea": echo "<tr>"; echo "<td valign='top'><strong>".$field['Title']."</strong></td><td>"; if( ($field['MaxChars']*1) > 0 ){ $max = $field['MaxChars']; $id = $field['Title'].'_message'; $additional_attributes = " onkeyup='check_length($max,this,\"$id\");' onChange='check_length($max,this,\"$id\");' onmouseout='check_length($max,this,\"$id\");' "; echo "<div><span id='$id'>$max</span> Characters Left</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746865 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 No, that is the form field. I mean where you do a mysql_query INSERT INTO xtable....potion, are you doing any data manipulation there? Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746870 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 Hi Where do I get that from? Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746887 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 This any good? //fetch db tables $tables = mysql_list_tables($database_myconn,$myconn); //for each table found while($tbl_row = mysql_fetch_row($tables)){ // Dump data $table = $tbl_row[0]; unset($data); $data = 'truncate table `'.$table.'`; '; $result = mysql_query("SELECT * FROM `$table`"); $num_rows = mysql_num_rows($result); $num_fields = mysql_num_fields($result); for ($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_object($result); $data .= "INSERT INTO `$table` ("; // field names for ($x = 0; $x < $num_fields; $x++) { $field_name = mysql_field_name($result, $x); $data .= "`{$field_name}`"; $data .= ($x < ($num_fields - 1)) ? ", " : false; } $data .= ") VALUES ("; // Values for ($x = 0; $x < $num_fields; $x++) { $field_name = mysql_field_name($result, $x); $data .= "'" . str_replace('\"', '"', mysql_escape_string($row->$field_name)) . "'"; $data .= ($x < ($num_fields - 1)) ? ", " : false; } $data.= ");\n "; } Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746910 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 $data .= "'" . str_replace('\"', '"', mysql_escape_string($row->$field_name)) . "'"; That is where your problem lies. You are replacing the \" with " and then escaping the string on data coming out of the DB. $data .= "'" . mysql_escape_string($row->$field_name) . "'"; Make it that and see if it makes the data appear right. If this does not fix it, then you need to find where the data is being entered from a FROM (post data) and post that section so we can help you fix that part. Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746920 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 Hi Thanks for taking the trouble to help on this but I did a search for INSERT INTO and found that code, its actually from the backup section!! sorry about that, I cannot seem to find any others what else can i search for within the script? Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746939 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 Hi Thanks for taking the trouble to help on this but I did a search for INSERT INTO and found that code, its actually from the backup section!! sorry about that, I cannot seem to find any others what else can i search for within the script? Is all the data already in the DB and you are just grabbing that data? Or is this data still able to be entered from a form? If it can be entered from a form, look at the form page and see where the "action" is going to and find that section to locate where the problem could be occuring. Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746950 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 There is data already there and is editable echo "<h4>Edit Ad</h4>"; echo "<form action='edit2.php' method=post>"; echo "<table cellpadding=2 cellspacing=0 width='90%'>"; echo "<tr >"; echo "<td colspan=2 class='subHeader'>*Fields will validate on submit</td>"; echo "</tr>"; FROM EDIT2.php if($_POST['TypeID'] != ""){ //verify type id $sql = sprintf("select * from types where ID = %s", intval($_POST['TypeID'])); $result = $mysql->exSql($sql) or die($mysql->debugPrint()); if(mysql_num_rows($result)<1){ die("<script>window.location='logout.php';</script>"); } //Fetch fields types to insert $node = new sqlNode(); $node->table = "customfields"; $node->select = "*"; $node->where = "where TypeID = ".intval($_POST['TypeID']); $node->orderby = "group by GroupID order by `Order` asc"; if( ($result = $mysql->select($node)) === false ) die($mysql->debugPrint()); $node = new sqlNode(); $node->table = sprintf("`tt_%s`", abs(intval($_POST['TypeID']))); $fetchLatLong = false; while($field = mysql_fetch_assoc($result)){ if( ($field['Title'] != "Latitude") && ($field['Title'] != "Longitude") ){ if($field['Type'] == 'checkbox') $node->push("text",$field['Title'],@implode(',',$_POST[str_replace(" ","_",$field['Title'])])); else{ if($field['AllowHTML'] == 'no'){ $node->push("text",$field['Title'],strip_tags($_POST[str_replace(" ","_",$field['Title'])])); } else{ $node->push("text",$field['Title'],$_POST[str_replace(" ","_",$field['Title'])]); } } } if($field['Title'] == "Latitude") $fetchLatLong = true; } if($fetchLatLong){if($_POST['TypeID'] != ""){ //verify type id $sql = sprintf("select * from types where ID = %s", intval($_POST['TypeID'])); $result = $mysql->exSql($sql) or die($mysql->debugPrint()); if(mysql_num_rows($result)<1){ die("<script>window.location='logout.php';</script>"); } //Fetch fields types to insert $node = new sqlNode(); $node->table = "customfields"; $node->select = "*"; $node->where = "where TypeID = ".intval($_POST['TypeID']); $node->orderby = "group by GroupID order by `Order` asc"; if( ($result = $mysql->select($node)) === false ) die($mysql->debugPrint()); $node = new sqlNode(); $node->table = sprintf("`tt_%s`", abs(intval($_POST['TypeID']))); $fetchLatLong = false; while($field = mysql_fetch_assoc($result)){ if( ($field['Title'] != "Latitude") && ($field['Title'] != "Longitude") ){ if($field['Type'] == 'checkbox') $node->push("text",$field['Title'],@implode(',',$_POST[str_replace(" ","_",$field['Title'])])); else{ if($field['AllowHTML'] == 'no'){ $node->push("text",$field['Title'],strip_tags($_POST[str_replace(" ","_",$field['Title'])])); } else{ $node->push("text",$field['Title'],$_POST[str_replace(" ","_",$field['Title'])]); } } } if($field['Title'] == "Latitude") $fetchLatLong = true; } if($fetchLatLong){ Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746966 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 Well if you can fix where the data gets entered, that is ideal. This will/should work for current data, however. It is just a band-aid, as stated you should really fix the problem at it's source: echo nl2br(str_replace('\n', "\n", $listing[$field['Title']])); And see if that does what you want. Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746971 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 Thanks, almost but ('\r\n', "\r\n" did the trick thing is, when you go back to edit it displays \r\n\r\n\ within the edit section (not the display section, it displays ok now) It'll do for now tho thanks a lot if you can think of a way to do it at source let me know thanks again Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-746995 Share on other sites More sharing options...
Zombies Posted January 26, 2009 Author Share Posted January 26, 2009 Managed to do the same in edit.php as well so it doesnt appear whilst editing et viola Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/#findComment-747005 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.