problem with str_replace and single quote

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hugo Coolens

    problem with str_replace and single quote

    I need to use the str_replace function for a php-script which works
    together with html/javascript.
    In the script I have to perform things like:
    $arabic=str_rep lace ("'","\'", $row[0]);
    $arabic=str_rep lace ("sh", "\š", $row[0]);

    the second line works perfectly, however I can't make the first one
    work, it should replace a single quote with a quote preceded by a
    backslash. I presume I have to escape some characters to make this
    happen however I can't figure out how.
    any help welcome
    best regards,
    Hugo

    b.t.w. I can't use the htmlspecialchar s-function as this will interfere
    with other code in the script

  • Chris Hope

    #2
    Re: problem with str_replace and single quote

    Hugo Coolens wrote:
    [color=blue]
    > I need to use the str_replace function for a php-script which works
    > together with html/javascript.
    > In the script I have to perform things like:
    > $arabic=str_rep lace ("'","\'", $row[0]);
    > $arabic=str_rep lace ("sh", "\š", $row[0]);
    >
    > the second line works perfectly, however I can't make the first one
    > work, it should replace a single quote with a quote preceded by a
    > backslash. I presume I have to escape some characters to make this
    > happen however I can't figure out how.
    > any help welcome
    > best regards,
    > Hugo
    >
    > b.t.w. I can't use the htmlspecialchar s-function as this will interfere
    > with other code in the script[/color]

    You need to escape the backslash with another backslash, otherwise you're
    escaping the single quote.

    $arabic=str_rep lace ("'","\\'", $row[0]);

    --
    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Hugo Coolens

      #3
      Re: problem with str_replace and single quote

      Chris Hope wrote:[color=blue]
      > Hugo Coolens wrote:
      >
      >[color=green]
      >>I need to use the str_replace function for a php-script which works
      >>together with html/javascript.
      >>In the script I have to perform things like:
      >>$arabic=str_r eplace ("'","\'", $row[0]);
      >>$arabic=str_r eplace ("sh", "\š", $row[0]);
      >>
      >>the second line works perfectly, however I can't make the first one
      >>work, it should replace a single quote with a quote preceded by a
      >>backslash. I presume I have to escape some characters to make this
      >>happen however I can't figure out how.
      >>any help welcome
      >>best regards,
      >>Hugo
      >>
      >>b.t.w. I can't use the htmlspecialchar s-function as this will interfere
      >>with other code in the script[/color]
      >
      >
      > You need to escape the backslash with another backslash, otherwise you're
      > escaping the single quote.
      >
      > $arabic=str_rep lace ("'","\\'", $row[0]);
      >[/color]
      Unfortunately this does not work, I already tried that, the problem
      seems to be the first single quote in the expression.

      regards,
      hugo

      Comment

      • Alvaro G Vicario

        #4
        Re: problem with str_replace and single quote

        *** Hugo Coolens wrote/escribió (Thu, 16 Sep 2004 09:51:28 +0200):[color=blue][color=green]
        >> $arabic=str_rep lace ("'","\\'", $row[0]);
        >>[/color]
        > Unfortunately this does not work, I already tried that, the problem
        > seems to be the first single quote in the expression.[/color]

        This works fine for me (I've just tested it). Are you sure there are
        actually single quote chars in your text and not only tildes like ` or ´?


        --
        -- Álvaro G. Vicario - Burgos, Spain
        -- Thank you for not e-mailing me your questions
        --

        Comment

        • Chris Hope

          #5
          Re: problem with str_replace and single quote

          Hugo Coolens wrote:
          [color=blue]
          > Chris Hope wrote:[color=green]
          >> Hugo Coolens wrote:
          >>
          >>[color=darkred]
          >>>I need to use the str_replace function for a php-script which works
          >>>together with html/javascript.
          >>>In the script I have to perform things like:
          >>>$arabic=str_ replace ("'","\'", $row[0]);
          >>>$arabic=str_ replace ("sh", "\š", $row[0]);
          >>>
          >>>the second line works perfectly, however I can't make the first one
          >>>work, it should replace a single quote with a quote preceded by a
          >>>backslash. I presume I have to escape some characters to make this
          >>>happen however I can't figure out how.
          >>>any help welcome
          >>>best regards,
          >>>Hugo
          >>>
          >>>b.t.w. I can't use the htmlspecialchar s-function as this will interfere
          >>>with other code in the script[/color]
          >>
          >>
          >> You need to escape the backslash with another backslash, otherwise you're
          >> escaping the single quote.
          >>
          >> $arabic=str_rep lace ("'","\\'", $row[0]);
          >>[/color]
          > Unfortunately this does not work, I already tried that, the problem
          > seems to be the first single quote in the expression.[/color]

          Worked when I tested it. What's the result you're getting?

          --
          Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

          Comment

          • John Dunlop

            #6
            Re: problem with str_replace and single quote

            Chris Hope wrote:
            [color=blue]
            > Hugo Coolens wrote:[/color]

            [ ... ]
            [color=blue][color=green]
            > > $arabic=str_rep lace ("'","\'", $row[0]);[/color][/color]

            [ ... ]
            [color=blue]
            > You need to escape the backslash with another backslash, otherwise you're
            > escaping the single quote.
            >
            > $arabic=str_rep lace ("'","\\'", $row[0]);[/color]

            No. '\\'' in a double-quoted string is equivalent to '\''.
            In the latter the backslash is taken literally, because '\''
            isn't an escape sequence; in the former the first backslash
            escapes the second, leaving a backslash and apostrophe.



            --
            Jock

            Comment

            • Chris Hope

              #7
              Re: problem with str_replace and single quote

              John Dunlop wrote:
              [color=blue]
              > Chris Hope wrote:
              >[color=green]
              >> Hugo Coolens wrote:[/color]
              >
              > [ ... ]
              >[color=green][color=darkred]
              >> > $arabic=str_rep lace ("'","\'", $row[0]);[/color][/color]
              >
              > [ ... ]
              >[color=green]
              >> You need to escape the backslash with another backslash, otherwise you're
              >> escaping the single quote.
              >>
              >> $arabic=str_rep lace ("'","\\'", $row[0]);[/color]
              >
              > No. '\\'' in a double-quoted string is equivalent to '\''.
              > In the latter the backslash is taken literally, because '\''
              > isn't an escape sequence; in the former the first backslash
              > escapes the second, leaving a backslash and apostrophe.
              >
              > http://www.php.net/manual/en/language.types.string.php
              >[/color]

              That's what he wanted, and I quote: "it should replace a single quote with a
              quote preceded by a backslash". So by escaping the backslash he ends up
              with a backslash followed by a single quote.

              --
              Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

              Comment

              • John Dunlop

                #8
                Re: problem with str_replace and single quote

                Chris Hope wrote:
                [color=blue]
                > That's what he wanted, and I quote: "it should replace a single quote with a
                > quote preceded by a backslash". So by escaping the backslash he ends up
                > with a backslash followed by a single quote.[/color]

                And not escaping the backslash leaves him with a backslash
                followed by an apostrophe too. ;o)

                I was pointing out that there's no need to escape the
                backslash, and that H. Coolens' assignment was equivalent to
                yours. My 'no' was disagreeing with your 'you need to
                escape the backslash with another backslash, otherwise
                you're escaping the single quote', because, in a double-
                quoted string, '\'' means a backslash followed by an
                apostrophe. No escaping occurs.

                Sorry if I wasn't clear.

                --
                Jock

                Comment

                • Chris Hope

                  #9
                  Re: problem with str_replace and single quote

                  John Dunlop wrote:
                  [color=blue]
                  > Chris Hope wrote:
                  >[color=green]
                  >> That's what he wanted, and I quote: "it should replace a single quote
                  >> with a quote preceded by a backslash". So by escaping the backslash he
                  >> ends up with a backslash followed by a single quote.[/color]
                  >
                  > And not escaping the backslash leaves him with a backslash
                  > followed by an apostrophe too. ;o)
                  >
                  > I was pointing out that there's no need to escape the
                  > backslash, and that H. Coolens' assignment was equivalent to
                  > yours. My 'no' was disagreeing with your 'you need to
                  > escape the backslash with another backslash, otherwise
                  > you're escaping the single quote', because, in a double-
                  > quoted string, '\'' means a backslash followed by an
                  > apostrophe. No escaping occurs.
                  >
                  > Sorry if I wasn't clear.[/color]

                  You're right for double quoted strings, which was in his example originally,
                  so I was wrong about that :) I think I was just assuming that was the
                  answer without checking "\'" myself. Funny that it wasn't working for him
                  in the first place then.

                  print "\'"; // this will output \'

                  But if you're using single quotes, as in your post, then you just get a
                  single quote:

                  print '\''; // this will output '

                  Cheers,
                  Chris

                  --
                  Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

                  Comment

                  • Hugo Coolens

                    #10
                    Re: problem with str_replace and single quote

                    Chris Hope wrote:[color=blue]
                    > Hugo Coolens wrote:
                    >
                    >[color=green]
                    >>Chris Hope wrote:
                    >>[color=darkred]
                    >>>Hugo Coolens wrote:
                    >>>
                    >>>
                    >>>
                    >>>>I need to use the str_replace function for a php-script which works
                    >>>>together with html/javascript.
                    >>>>In the script I have to perform things like:
                    >>>>$arabic=str _replace ("'","\'", $row[0]);
                    >>>>$arabic=str _replace ("sh", "\š", $row[0]);
                    >>>>
                    >>>>the second line works perfectly, however I can't make the first one
                    >>>>work, it should replace a single quote with a quote preceded by a
                    >>>>backslash . I presume I have to escape some characters to make this
                    >>>>happen however I can't figure out how.
                    >>>>any help welcome
                    >>>>best regards,
                    >>>>Hugo
                    >>>>
                    >>>>b.t.w. I can't use the htmlspecialchar s-function as this will interfere
                    >>>>with other code in the script
                    >>>
                    >>>
                    >>>You need to escape the backslash with another backslash, otherwise you're
                    >>>escaping the single quote.
                    >>>
                    >>>$arabic=str_ replace ("'","\\'", $row[0]);
                    >>>[/color]
                    >>
                    >>Unfortunate ly this does not work, I already tried that, the problem
                    >>seems to be the first single quote in the expression.[/color]
                    >
                    >
                    > Worked when I tested it. What's the result you're getting?[/color]
                    I discovered my mistake, I just wast overwriting my variable again and
                    again, therefore I didn't get the expected result.
                    Thank you all for the comments on escaping and mea culpa, mea culpa mea
                    maxima culpa

                    hugo

                    Comment

                    Working...