php - nl2br() creates an extra new line -
i trying insert description text field database.
i doing this:
$group_description = mysql_real_escape_string($_post['group_description']);
but creating problems because when taking things out of database, being displayed \n\r strings instead of new lines.
here example of page problem:
http://www.comehike.com/hikes/hiking_group.php?hiking_group_id=48
so tried fix adding nl2br this:
$group_description = mysql_real_escape_string(nl2br($_post['group_description']));
but inserted line :( here example of current problem:
http://www.comehike.com/hikes/hiking_group.php?hiking_group_id=50
here example of insert statement use:
$insert_group_sql = 'insert hiking_groups( title , group_description ) values ( "'.$group_name.'" , "'.$group_description.'" ) ';
what proper way this?
here code use display $group_description
//convert urls links $group_description = preg_replace('#([\s|^])(www)#i', '$1http://$2', $group_description); $pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i'; $replacement = '<a href="$1" target="_blank">$1</a>'; $group_description = preg_replace($pattern, $replacement, $group_description); $group_description = str_replace("\'" , "'", $group_description ); $group_description = nl2br($group_description); /* convert e-mail matches appropriate html links */ $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.'; $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i'; $replacement = '<a href="mailto:\\1">\\1</a>'; $group_description = preg_replace($pattern, $replacement, $group_description);
first, suggest spudley suggests, keep in original form in database , format once display it.
nl2br()
should work, suggest check input data see being input (which easier if don't format before store in db). takes \n, \n\r, \r\n , \r , inserts <br/>
instead. should check instance there no space in between \n , \r.
also, make sure use nl2br()
in 1 place, since doesn't replace new lines, inserts <br/>
(that is, if nl2br(nl2br($group_description))
2 <br/>
)
update:
i see in additional code when display description, have nl2br()
. need remove 1 of them, add <br/>
s once.
also, instead of this:
$group_description = str_replace("\'" , "'", $group_description ); $group_description = nl2br($group_description);
try this:
$group_description = stripslashes($group_description); $group_description = nl2br($group_description);
that should remove sanitizing mysql_real_escape_string()
did, should solve problem of \n\r showing in text.
Comments
Post a Comment