Jump to content

Recommended Posts

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;

Link to comment
https://forums.phpfreaks.com/topic/142425-solved-nl2br-problems/
Share on other sites

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 ;)

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

 

 

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.

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>";             

            }

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

";

}

               $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.

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.

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){

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.

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

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.