Formatting data being written to MySQL...
Hopefully the last question :p
Two issues
1. Have a text area where the operator can enter whatever and it then gets written to the database. Unfort it's not including paragraph breaks :eek: Is there a function in PHP to replace line breaks with <br /> or something similar.
2. Names entered as O'Brian are appearing as O\'Brian when read from the database, does PHP have a character escape function, cause I guess that's what I need.
Re: Formatting data being written to MySQL...
1. you could use nl2br(). this replaces every new line to an HTML line break. alternatively, and mostly just because I have a thing against "<br />", I do this:
PHP Code:
$body = "<p>" . $body . "</p>";
$body = str_replace("\n\r", "</p><p>", $body);
$body = str_replace("\n", "", $body);
$body = str_replace("\r", "", $body);
2. use stripslashes() on any POST/GET variables when you have magic_quotes_gpc on before entering them into your queries.
Re: Formatting data being written to MySQL...
kows,
Just to verify.
I use both methods before writing the data away to the database or after retrieving it from the database?
Thanks for the help btw, the stupid software wont allow me to add to your rep :ehh:
Re: Formatting data being written to MySQL...
didn't get to check this until just now.
I generally store all information in the database as plain text, and apply any line-breaking methods when displaying it. This makes it easy to be used in different situations, if those situations ever occur. I think that some forum softwares keep a plain text version and an HTML version in the database, so that extra processing is not needed every time they want to display the information. if you want, you could do that, too.
for inserting to the database, I always make a variable calling mysql_real_escape_string() to make sure there are slashes where there needs to be. if you're displaying the information and still have slashes, you can use stripslashes() when you're displaying it.
PHP Code:
<?php
$var_to_insert = mysql_real_escape_string($_POST['var']);
?>
<?php
$var_to_display = stripslashes($result['var']);
echo $var_to_display;
?>
Re: Formatting data being written to MySQL...
Cool, off to use my new knowledge to combat the forces of darkness :wave:
Thanks once again kows :thumb: