We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,985 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

rawurldecode() doesn't take in to account newline (CRLF) chars

Hi, I have a form which saves a string of text to a database with rawurlencode() which adds things like space characters (%20) and newline characters (%0D%0A), however, when I use rawurldecode() on this string it only decodes the space characters, and ignores all the newlines, by simply printing them also as spaces.

If there's another way to ensure new paragraph lines are printed correctly I'm all ears but I've spent the last hour or two asking/googling and have found no workable solution, I've also played around with str_replace() and nl2br() to no avail.

2
Contributors
5
Replies
18 Hours
Discussion Span
11 Months Ago
Last Updated
6
Views
Question
Answered
Red Dragon
Newbie Poster
12 posts since Oct 2004
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

Newlines do not print if you echo them directly to the screen. Can't you pass this via form/post - does it have to be via url/get?

diafol
Keep Smiling
Moderator
10,653 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

it's directly from a mysql_query

$fetch_news = mysql_query("SELECT id, post_user, post_date, post_msg FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_array($fetch_news)) { echo rawurldecode($row[3]); }

I've also tried:

str_replace("%0D%0A", "\r\n", $row[3]); // for newlines

And:

str_replace("%20", " ", $row[3]); // for spaces

They work seperately, but I need them to work at the same time, also I don't feel like doing a str_replace() for EVERY possible ASCII character when I would've thought rawurldecode() should handle it by itself.

Red Dragon
Newbie Poster
12 posts since Oct 2004
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

They work seperately, but I need them to work at the same time, also I don't feel like doing a str_replace() for EVERY possible ASCII character when I would've thought rawurldecode() should handle it by itself.

Have you tried running

$output = nl2br(rawurldecode($str));

I don't understand why you're saving these strings in encoded form in the DB. It just seems a little awkward to me. If you really have to pass data via querystring, which I don't quite understand, you could do the decode prior to storing?

diafol
Keep Smiling
Moderator
10,653 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

that worked great thanks diafol, it's encoded for storing to that spaces/newlines etc are saved as they should be (rather than just one long line). i was just struggling to get the output the same as the input

Red Dragon
Newbie Poster
12 posts since Oct 2004
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

Ok, we solved?

diafol
Keep Smiling
Moderator
10,653 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57
Question Answered as of 11 Months Ago by diafol

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0774 seconds using 2.72MB