php - Why would \n\r appear if I used nl2br()? -
someone made group on site has description \n\r
. thing description text:
//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);
you can see happening here.
you see says there \n\r welcome join
.
why didn't become <br />
tag?
this important, here how put text db in first place:
$group_description = mysql_real_escape_string($_post['group_description']); // , insert $group_description db without doing more operations on it.
any idea why still getting \n\r
?
the problem \r , \n stored in database "\" "r" , "\" "\n" respectively. they're not stored carriage return or new line character slash character followed letter. when you're calling nl2br()
, it's looking new line character, not textual representation of it. makes sense.
you use regular expression (preg_replace
) or str_replace
replace them. problem caused how data inserted in database.
for instance:
$string = str_replace('\n', "\n", $string); $string = str_replace('\r', "\r", $string);
you'll notice used single quotes first 1 , double second. when using single quotes, "\r" , "\n" stored text, not special characters. why they're not stored in database.
Comments
Post a Comment