SuperBlue Posted January 12, 2008 Share Posted January 12, 2008 I need to replace known "Linebreaks characters" with something like '</p><p class="SoftBreak">' and '</p><p class="HardBreak">'. The thing is, i havent been able to find any documention on existing "linebreaks characters", and i dont really know the differance between \n and \r. Atm im using str_replace to replace \n and \r with br tags. I have made a script, which is showing entries from a database, these entries are user submitted. As such atm, im just saving them in their original format, and doing the replace before they are displayed. If i was more shure about this, i could just replace them for good, when the user hits submit. And perhaps save some server resources that way? In any case, i would still like to know more about this, which method is best?, and what are the diffrent LineBreaks which needs to be replaced? Edit> Also, some users like to "fill out" a bunch of linebreaks, so how could i strip out any, or unnessary linebreaks at the end of a post?, those that would otherwhise create a bunch of empty tags. Solved> Answer by Daukan accepted. As for the last part, well i found that trough the link provided by Daukan To remove linebreaks and whitespace at the end of a string use rtrim. To do the same with the start of a string, simply use ltrim and with both the end and start use trim Quote Link to comment Share on other sites More sharing options...
Daukan Posted January 12, 2008 Share Posted January 12, 2008 nl2br is a built in function in PHP to change new lines to breaks. str_replace should be able to add those <p> tags though. <?php $s = 'hello'."\n".'goodbye'."\n"; $formated_s = '<p class="SoftBreak">'.str_replace("\n", '</p><p class="SoftBreak">'."\n", $s)."</p>"; echo $formated_s; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.