SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Keep text formatting

    Hi,

    Im trying to make a cms but am having problems with the following.

    I have a section where you can upload an article. At the moment i have to type it out as html for it to view correctly on my site i.e. i have to add <p> tags.

    If i type something like:
    PHP Code:
    <p>Hello</p>

    <
    p>world</p
    It will come up on my site as <p>Hello</p>rnrn<p>world</p>. the line break is coming up as rnrn as i have used stripslashes() to remove unwanted / when someone enters something like "what's".

    Really im trying to do a few things at once and they are interfering with each other so im hoping someone can point me in the right direction for a way to insert articles and then have them print out on my site correctly.

    Any help would be greatly appreciated.

    Thanks

  2. #2
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried with nl2br()?

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    get rid of the stripslashes when the info goes INTO the databse, you shoudl be using mysql_real_escape_string() instead. When you echo the data onto the page, use striplashes(nl2br($rows['text_from_table'])) that will remove the slashes and keep the formatting
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you need stripslashes() at all. Use mysql_real_escape_string(), as was suggested, when submitting data to the database; and nl2br() on output.

  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, if you are stlll using addslashes, that must be replaced, as mentioned by other guys.

    Besides, if you are thinking to give something fancy to your users, you can also try tinymce wysiwyg.

  6. #6
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe View Post
    Besides, if you are thinking to give something fancy to your users, you can also try tinymce wysiwyg.
    Thanks, got it working now. I was thinking it would be handy if their was a pho function for adding <p> tags automatically instead of having to add then whenever im inserting a new article. Is their such a thing?

    What are tinymce wysiwyg? You have me all intrigued now

  7. #7
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just thought... i could replace the <br> tags with </p> with preg_replace. I'll give that a lash.

  8. #8
    SitePoint Zealot tconley79's Avatar
    Join Date
    Feb 2004
    Location
    Miami
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had this situaiton a while back. What I did was perform two replacements:

    I first replaced all instances of two nl's in a row with "</p><p>".
    I then replaced any single left over nl's with a "<br />"

    That gave me my line and paragraph breaks.

    To finish it up, I wrapped the entire text in a "p" tag.

  9. #9
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Banana Man View Post
    What are tinymce wysiwyg?
    Hi, tinymce is an open source project, see here => http://tinymce.moxiecode.com/

  10. #10
    SitePoint Addict CommanderZ's Avatar
    Join Date
    Apr 2006
    Location
    Czech Republic
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FCKeditor is imo much better wysiwyg than TinyMCE, but it is a lot more complex.

  11. #11
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have used both. I found FCK was longer to load.

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I have used them both too and found that FCK was a massive drain on resources. Also TinyMCE doesn't play well if you have a textarea inside a div that scrolls, on IE7 it leaves the toolbar in a fixed position whilst the rest of the content moves!

    Both are good though
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  13. #13
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Grrrrr

    What syntax do you use to replace two <br /> ?????

    I am using the following:
    PHP Code:
    //replace the <br /> tags with </p>
            
    $punc[0]="/<br /><br />/";
            
            
    $current_article=preg_replace($punc,"</p>",$current_article);
            
    // replace all instances of any punctuation found in the $punc array with "" 
    but i get the following error:

    Warning: preg_replace() [function.preg-replace]: Unknown modifier '>' in /domains/mysite.ie/public_html/folder1/folder2/index.php on line 135

  14. #14
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to escape /s, like this.
    PHP Code:
    $punc[0]="/<br \/><br \/>/"
    Else, you may try str_replace, you don't need to escape the /s there.

  15. #15
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    391
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Doh,

    I've tried that now with str_replace and preg_replace but its still not working. Its not giving me the above error anymore so thanks for the advice above.

    It doesnt seem to be picking up the <br /><br />.

    When i look at the source code it would look something like this:

    <p>Hello, this is a test<br />
    <br />Hello, this is also a test
    </p>

    Is it something to do with the <br />'s appearing on two seperate lines maybe?

  16. #16
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then, try replacing the \n directly without passing through nl2br first. See the post of tconley79 above. That should solve the issue.

  17. #17
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    There's a load of userland code out there called nl2p(), google for it.

    There even used to be examples in the man http://www.php.net/nl2br


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •