Hi, I've got a news script I'm working on. When I type into the text box: "Hello. This is a test." I'd like the script to automatically add <br /> where the new line is. How can I do this?
Sorry to bust in on this thread, but can you, Jestep, create a very small example of this php code, with a bit more detail? Like an actual (very small) "working model"?
Thanks, that done it espmartin: $full = nl2br(cleanfunction($_POST['full'])); mysql_query("INSERT INTO articles (title, short, full, time) VALUES ('" . $title ."', '" . $short . "', '" . $full . "', '" . $time . "')") or die(mysql_error());
Actually, I'm having a problem with this conflicting with my cleanfunction. function cleanstring($string) { if(get_magic_quotes_gpc()) { $string = stripslashes($string); } elseif(!get_magic_quotes_gpc()) { $string = addslashes($string); } $string = strip_tags($string); $string = @mysql_real_escape_string($string); return $string; } PHP: nl2br(cleanstring($_POST['full'])); PHP: However, when posting: Hello Hello Hello The script actually outputs: HellornrnHellornrnHello..
It looks like the stripslashes( is messing up your input. Since a line break looks like a \r or \n, the strip slashes is removing the \ and you end up with a r or n. nl2br looks for the \n \r and cant find them so it doesn't do anything.
You could add the <br /> before you do anything else with that specific input. cleanstring(nl2br($_POST['full']));
worth telling you about though. $test=$_POST['test']; $test = preg_replace("/\n/","\n<BR>\n",$test); Code (markup): I'm a newb and I'm sure lotsa people can poke holes in this, but there it is. Also, I may be TOTALLY missing the point on this, but it seems like the answer to your question may have been complicated. the simple way to use nl2br would be similar to my above code: $test=$_POST['test']; $test = nl2br($test); Code (markup): -- ~alhen
Well i would suggest you using the nl2br after querying the database not putting in the <br /> 's in the database. This will be more flexible for you to just use the /n as its better supported in php than the html tag. However its your choice .