Using line breaks in mysql

danielneridanielneri WP V.I.P.VPS - Virtual Prince of the Server
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
ban1.gif

Comments

  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    Yes, this is possible, but you use PHP to do it, not mySQL...
    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.
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    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!!!
  • ChroderChroder Senior Member The Royal RAM
    Use nl2br().

    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).
  • danielneridanielneri WP V.I.P. VPS - Virtual Prince of the Server
    but nuvo when i use str_replace, it replaces all the br's also, so therefore it doesnt line break anything

    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?
    ban1.gif
  • ChroderChroder Senior Member The Royal RAM
    You must be passing it to htmlspecialchars() or related function for this to happen.

    The string returned by nl2br() will be valid, renderable HTML.

    [php]$str = "Hello\nWorld.";

    $str = nl2br($str);

    // $str is now:
    // Hello
    // <br />World.[/php]
  • danielneridanielneri WP V.I.P. VPS - Virtual Prince of the Server
    $message = $_POST;
    $message = nl2br($message);
    $message = str_replace("<","<",$message);
    $message = str_replace(">",">",$message);

    thats my code

    but its gets all screwed
    ban1.gif
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    nl2br is PHP's inbuilt method for translating textarea linebreaks to the line break HTML tag, <br />.
    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]
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    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!!!
  • danielneridanielneri WP V.I.P. VPS - Virtual Prince of the Server
    ah thanks so much, will try it right now


    nuvo you are a genious!!!!

    thanks sooo much....again!
    ban1.gif
Sign In or Register to comment.