I have a php script which adds news to a database. But i have no idea how to automatically insert the enters(line breaks) i use. for example:
if i typed in this:
Hello,
World
ill get this as a result:
Hello,World
is there any way to automatically include line breaks typed in the textarea from php?
thanks
Comments
As I mentioned in one of the other topics, you need to pre-parse content before insertion using PHP's string modifiaction routines.
HTML's textarea object uses the fairly standard /n line break indicator.
If you use str_replace() to change all instances of /n to <br />, you should be able to add linebreak functionality with ease.
Current project: CMS Object.
Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
Release date: NEVER!!!
If you were to use str_replace, remember you must search and replace not only \n, but \r and \r\n as well (\n for *nix, \r\n for Windows, \r for Mac).
Webmaster-Talk.com
Chroder.com
ok chroder i used nl2br, but look what happened
hello<br /> world<br /> this<br /> is amaZin<br /> g<br /> <?php<br /> echo 'hi';<br /> ?>
its not rendering the br's, instead its echoing them.
any ideas?
The string returned by nl2br() will be valid, renderable HTML.
[php]$str = "Hello\nWorld.";
$str = nl2br($str);
// $str is now:
// Hello
// <br />World.[/php]
Webmaster-Talk.com
Chroder.com
$message = nl2br($message);
$message = str_replace("<","<",$message);
$message = str_replace(">",">",$message);
thats my code
but its gets all screwed
The main problem with using PHP's inbuilt method is that if you're running an old version of PHP, you may end up getting HTML 4.01 style line breaks, which invalidate XHTML code since XHTML requires that all tags without a closing tag have the self closing slash (this is one of the things carried over from XML, and it's fairly logical, if not quite as lazy).
You can't use the manual swapping method and PHP's inbuilt method together as there's the chance that, if you use PHP's inbuilt function first, you'll see the line breaks written as text and using the manual method and then the PHP method is pretty pointless really.
The problem with the code you just posted is that you're converting the linebreaks to HTML and then stripping them out with string replacements designed to kill off any HTML tags.
The method of tag stripping you have used (which is what I suggested previously) will stop all tags within the variable you're editing from functioning, which is usually a good thing, but which can also end with you using a BBCode-esque parser to allow better formatting (BBCode parsers are quite simple to write if you learn to use preg_replace();).
Basically, change that code to:
[php]
$message = $_POST; //Get data to edit and put it in the variable.
$message = str_replace("<","<",$message); //Get rid of these and replace with the ASCII character code used to reprisent it.
$message = str_replace(">",">",$message); //Same as above.
$message = nl2br($message); //Since we aren't stripping /n, the changes above haven't changed how the line breaks will function, but they won't be stripped out if you put them in last.
[/php]
Current project: CMS Object.
Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
Release date: NEVER!!!
nuvo you are a genious!!!!
thanks sooo much....again!