Jump to content

[SOLVED] Working with User submitted Linebreak Characters


SuperBlue

Recommended Posts

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

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.