ereg_replace eat slash

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

    ereg_replace eat slash

    Someone could tell me why preg_replace eat a slash when it do replacement?

    <?
    $stringToReplac e = "blah blah {nome} blah blah";
    $replacement = "Two slash: \\\\";
    $stringToReplac e =
    preg_replace("/{nome}/Us",$replacemen t,$stringToRepl ace,1);
    echo($stringToR eplace);

    #
    # It print
    # blah blah Two slash: \ blah blah
    #
    # instead of
    # blah blah Two slash: \\ blah blah
    #
    # Why it eat a slash?
    #
    ?>
  • Pedro Graca

    #2
    Re: ereg_replace eat slash

    Xaradas wrote:[color=blue]
    > Someone could tell me why preg_replace eat a slash when it do replacement?
    >
    > <?
    > $stringToReplac e = "blah blah {nome} blah blah";
    > $replacement = "Two slash: \\\\";[/color]

    internaly, $replacement is "Two slash: \\"
    [color=blue]
    > $stringToReplac e =
    > preg_replace("/{nome}/Us",$replacemen t,$stringToRepl ace,1);[/color]

    Lets put $replacement contents inside the preg_replace (as php will do)

    preg_replace("/{nome}/Us","Two slash: \\",$stringToRe place,1);
    [color=blue]
    > echo($stringToR eplace);
    >
    > #
    > # It print
    > # blah blah Two slash: \ blah blah[/color]

    Right! preg_replace() evaluates the replacement like an ordinary string.

    Try

    $replacement = "Two slash: \\\\\\\\"; /* redoubled because */
    /* it will be used twice */
    [color=blue]
    > ?>[/color]

    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!

    Comment

    • Xaradas

      #3
      Re: ereg_replace eat slash

      Pedro Graca wrote:[color=blue]
      > Xaradas wrote:
      >[color=green]
      >>Someone could tell me why preg_replace eat a slash when it do replacement?
      >>
      >><?
      >> $stringToReplac e = "blah blah {nome} blah blah";
      >> $replacement = "Two slash: \\\\";[/color]
      >
      >
      > internaly, $replacement is "Two slash: \\"
      >
      >[color=green]
      >> $stringToReplac e =
      >>preg_replace( "/{nome}/Us",$replacemen t,$stringToRepl ace,1);[/color]
      >
      >
      > Lets put $replacement contents inside the preg_replace (as php will do)
      >
      > preg_replace("/{nome}/Us","Two slash: \\",$stringToRe place,1);
      >
      >[color=green]
      >> echo($stringToR eplace);
      >>
      >> #
      >> # It print
      >> # blah blah Two slash: \ blah blah[/color]
      >
      >
      > Right! preg_replace() evaluates the replacement like an ordinary string.
      >
      > Try
      >
      > $replacement = "Two slash: \\\\\\\\"; /* redoubled because */
      > /* it will be used twice */
      >
      >[color=green]
      >>?>[/color]
      >
      >[/color]

      I've check your solution and it's ok but why using
      ereg_replace I don't need to redouble?

      <?

      $stringToReplac e = "blah blah {nome} blah blah";
      $replacement = "Two slash: \\\\";
      $stringToReplac e = ereg_replace("{ nome}",$replace ment,$stringToR eplace);
      echo($stringToR eplace);

      # It print
      # blah blah Two slash: \\ blah bla

      ?>

      Comment

      • Chung Leong

        #4
        Re: ereg_replace eat slash


        "Xaradas" <NOSPAM@NOSPAM. NO> wrote in message
        news:O2zDd.6521 65$35.26451258@ news4.tin.it...[color=blue]
        > Someone could tell me why preg_replace eat a slash when it do replacement?[/color]

        That's because the replacement string isn't treated simple text.
        preg_replace() can insert captured result into the replacement string. For
        sample, preg_replace('/I hate (\w+)/', 'I love \1', 'I hate Perl!') would
        yield "I love Perl!" Because the backslash is used to indicate the captured
        string, there needs to be a way of escaping it, in case you really want
        '\1'.


        Comment

        Working...