javascript - Replacedisplay new line from MYSQL to PHP - Stack Overflow

I have a big problem displaying line breaks from mysql in my php generated html page.I tried out nl2br

I have a big problem displaying line breaks from mysql in my php generated html page. I tried out nl2br(), manual str_replace() and so on... In my sql database the record is like:

first line
second line
third line

BUT: There are no \n's or <br>'s in that record. If I get that data via an ajax call and using javascript: data.mystring.replace(/\n/g, "<br />") on it, everything works fine!

But now I have to echo that data directly via php. And I don't see any line breaks. NO matter what I try. Any idea?

Here's the script (inserted via an ajax call):

$adress = mysqli_real_escape_string($con, $_POST["adress"]);
mysqli_query($con, "INSERT INTO Contact SET Adress = '$adress');

Reading the data of the database on my php page:

mysqli_query($con, "SET NAMES 'utf8'");
$query = mysqli_query($con, "SELECT * FROM Contact");
$dsatz = mysqli_fetch_assoc($query);
$newString = $dsatz["Adress"];

echo nl2br($newString);

I have a big problem displaying line breaks from mysql in my php generated html page. I tried out nl2br(), manual str_replace() and so on... In my sql database the record is like:

first line
second line
third line

BUT: There are no \n's or <br>'s in that record. If I get that data via an ajax call and using javascript: data.mystring.replace(/\n/g, "<br />") on it, everything works fine!

But now I have to echo that data directly via php. And I don't see any line breaks. NO matter what I try. Any idea?

Here's the script (inserted via an ajax call):

$adress = mysqli_real_escape_string($con, $_POST["adress"]);
mysqli_query($con, "INSERT INTO Contact SET Adress = '$adress');

Reading the data of the database on my php page:

mysqli_query($con, "SET NAMES 'utf8'");
$query = mysqli_query($con, "SELECT * FROM Contact");
$dsatz = mysqli_fetch_assoc($query);
$newString = $dsatz["Adress"];

echo nl2br($newString);
Share Improve this question edited Dec 20, 2016 at 18:59 Inkperial asked Dec 20, 2016 at 18:42 InkperialInkperial 831 silver badge9 bronze badges 8
  • Well if that regex works then there is a \n in the database data or maybe its a \r\n if you are unixer – RiggsFolly Commented Dec 20, 2016 at 18:45
  • 4 nl2br() just works so the problem is in your data/script. Show your script/markup. – bassxzero Commented Dec 20, 2016 at 18:46
  • Do you have Notepad++ or and editor that will show newline characters? – bassxzero Commented Dec 20, 2016 at 18:50
  • Nope, working on a mac – Inkperial Commented Dec 20, 2016 at 19:02
  • 1 "adress" is for speeches. "address" is for locations. Also, @RiggsFolly is probably correct. You may need to str_replace("\r\n",'<br />', $address) – CptMisery Commented Dec 20, 2016 at 19:04
 |  Show 3 more ments

2 Answers 2

Reset to default 4

As no \n shown in your db you can do

UPDATE tab SET yourmentcolumn= REPLACE(yourmentcolumn, CHAR(13), '\\n');

the CHAR() takes an ASCII code for carrige return 13 (or you can replace it with newline with code 10 if you mean newline) and the \\n is to escape the \n.

If you manage to replace them, you can then:

echo nl2br($yourString)

You can lookup over each character and check the value of it with the ord PHP function.

Check the value against an ascii table and see what value your line separator is.

You can use str_split or access a string like an array in a loop and check each character individually.

ord documentation: http://php/manual/en/function.ord.php chr (contains a link to ascii table) documentation: http://php/manual/en/function.chr.php Strings (String access and modification by character section) documentation: http://php/manual/en/language.types.string.php And str_split function: http://php/manual/en/function.str-split.php

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745040563a4607782.html

相关推荐

  • javascript - Replacedisplay new line from MYSQL to PHP - Stack Overflow

    I have a big problem displaying line breaks from mysql in my php generated html page.I tried out nl2br

    8小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信