I'm trying to make a few things, lets say a comment system for example, I got it so that it filters out illegal html inputted by the user such as javascript or something. But if I enter a comment and skip a line, when I retrieve the comment or whatever from the database it gets all jumbled up. How do I convert line breaks to <br /> tags?
This is a variation on the problem of dealing with the problem with too many newlines in text. The following bit of PHP code should do the trick: $string = preg_replace("/[\r\n]+/", "<br />", $string); Code (markup): This will work on mac, *nix and windows.
If you want to avoid the recently announced PCRE vulnerability, use str_replace instead. You do have to use several ("\r\n" "\r" "\n" in that order...) instead of just the one regexp. It all depends on what version of PHP/PCRE your host is using.
Yeah, just to give a spelled out example... $x = str_replace("\r\n","\n",$x); $x = str_replace("\r","\n",$x); $x = str_replace("\n","<br>",$x); Just out of interest, this allows parsing of different OS text files with the same code after the first couple steps.