Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

php - How to use nl2br() to handle string with ' '?

I am retrieving a product description value stored in database from admin through textarea upon form submit. When I select the description from database I get $description = $row['description']; and I would like to echo $description on main page like this: echo nl2br($description); but I see " " characters instead of making new rows. From what I've found here and on the net, your string must be used between double quotes, like this:

echo nl2br("Hello, 
 This is the description");

Now, the value of $description from database is in fact "Hello, This is the description" but in my script I have to use it like this:

echo nl2br($description);

Which does not make br's, it is outputing instead. So, what can I do, I can't use double quotes here, from my experience.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could translate them into their respective escape sequences before passing the string through nl2br(), like this:

$description = nl2br(str_replace('\r\n', "
", $description));

But what are the literal escapes doing in your database in the first place?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...