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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.