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.

Member Avatar for diafol

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?

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.

Member Avatar for diafol

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?

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

Member Avatar for diafol

Ok, we solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.