SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with line breaks

    Hi,

    I have searched the web for an answer, but couldn't find one. So I'm hoping you guys could help.

    I have a textarea field in a form that gets inserted into a MySQL database. The field is coded as a blob.

    Before the data gets inserted (or updated), I stripslash the data then use the mysql_real_escape_string function.

    The eventual use for this data is to be displayed on the screen as a toolltip when a visitor mouses over some highlighted text. I'm using javascript for this tooltip.

    Some of the data is escaped properly, such as apostrophies and quotes, but when the javascript encounters a line break, I get an unterminated string constant error and the tooltip will not display.

    When I go into the data and manually remove the line break, the tooltip works fine. However, this is not the solution I want.

    I was under the impression that the mysql_real_escape_string would properly escape the line breaks, but I guess not.

    Please let me know if I need to provide more information.

    Thank you in advance,
    Mark

  2. #2
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can remove the line breaks with str_replace("\n", "",$strVariable). The first argument is the string you want to replace, second is the string you want to replace it with, and third is the string you want the replace to happen in.

    You can either do this before you insert or update the db, or if you need the new line, you can do it after you pull the data out of the db.

    Hope this helps,
    Itshim

  3. #3
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    $tooltip = htmlspecialchars($tooltip);
    $tooltip = eregi_replace('\n','','$tooltip');
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, but it didn't work. I'm getting the same results.

    Just to clarify, I want the line breaks to display in the tooltip just as they were entered into the database. Maybe I've confused myself with all the changes I've been making. Here is the sequence as it now exists:

    When updating the data:

    PHP Code:
    $comment   stripslashes($_POST[$comm]);
      
    $comment   mysql_real_escape_string($comment); 
    When I display the data:

    PHP Code:
    $disp_comment addslashes(htmlspecialchars($disp_comment)); 
    Mark

  5. #5
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need this statement prior to inserting the data into the database:
    Code:
    $comment   = mysql_real_escape_string($comment);
    That's just adding the slashes back into the field that you just removed! Leave that first statement in that group as is, then insert the data into your database.

    The code I gave you is strictly for formatting the output for display. I know it works because I copy/pasted it directly into my post from some production code.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi vinyl-junkie,

    Okay, I'm not quite there yet, but I think I'm getting close.

    My shared server has magic quotes turned on. So when inserting (or updating) into the database, I have removed the stripslashes and mysql_real_escape_string functions that I originally had. Then when I display the output, I have your code exactly as you have it. Actually I removed the single quote from around the string name in the eregi_replace function.

    The result is that the tooltips with a line break still do not show, and the tooltips that do show have all the 'n' removed.

    I am assuming that In the textarea, when a user hits the enter key, that the only code entered is an /n. Is there maybe a /r as well that I am missing?

    Thanks again for your help .

    Mark

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinyl-junkie
    Try this:
    Code:
    $tooltip = htmlspecialchars($tooltip);
    $tooltip = eregi_replace('\n','','$tooltip');
    The ereg_* functions are very slow and there is almost no reason to ever use them. If you need to use regular expressions use the preg_* functions. However, in this case all you want is a simple string replacement. Also, '\n' will only replace an occurrence of \ and n, not a line break. This is what you want:

    PHP Code:
    <?php

    $tooltip 
    str_replace "\n"''$tooltip );

    ?>
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, this is now driving me crazy.

    I made the change to str_replace.

    For a tooltip that is currently displaying properly, I go into the administrative system I have and add a couple of carriage returns then type in some text to the bottom of the tooltip. When I display the report, that tooltip does not display.

    When I remove the carriage returns and text, the tooltip displays properly again.

    Any thoughts?

    Thanks again for everyone's help.

    Mark

  9. #9
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You maybe getting a newline and a carrage return from the textarea. On my system when I look at the data in the mysql table it will either have one or two little squares where the new line is. If this is the case you need to replace both:

    [code]
    $tooltip = str_replace("\n\r", '', $tooltip );
    [\code]

    I always forget which one comes first, so it maybe "\r\n" instead.

    Hope this helps.
    Itshim

  10. #10
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mac: \r
    win: \r\n
    *nix: \n

    also note what seanf said - single quotes will only match the literal sequence. to match that type of line break, use double quotes

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, the last suggestions worked for me. Thanks to all who helped.

    Since I wanted the line breaks to appear as intended, I replaced the /r/n with <br /> and acheived the result I wanted.

    I have other issues with the data in the tooltips, but for now I am very happy.

    Mark

  12. #12
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    The ereg_* functions are very slow and there is almost no reason to ever use them.
    Thanks for the tip, Sean. It's nice to know there's a better way of doing this. I made the appropriate changes to my script.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  13. #13
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nl2br probably would have been too simple


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
  •