back slashes and preg_replace's e modifier

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

    back slashes and preg_replace's e modifier

    Can anyone help me understand the following scripts output?:

    <?
    echo preg_replace('/.*/e',"sprintf('\\ \\\')",'');
    ?>

    The output is \ and this doesn't make a lot of sense to me.

    Since \\'s in strings are interpreted as single \'s, the above sprintf
    parameter is equal to \\\. The following scripts output confirms this:

    <?
    echo "sprintf('\\\\\ ')";
    ?>

    Attempting to run the output of the second script (as done in the next
    script), however, results in a syntax error:

    <?
    echo sprintf('\\\');
    ?>

    The fact that a syntax error would result isn't particurarly surprising
    since the last ' is being escaped. What is surprising, however, is
    that the last script yields a syntax error whereas the first script
    doesn't. Any ideas as to why?

  • Oli Filth

    #2
    Re: back slashes and preg_replace's e modifier

    yawnmoth said the following on 05/11/2005 22:29:[color=blue]
    > Can anyone help me understand the following scripts output?:
    >
    > <?
    > echo preg_replace('/.*/e',"sprintf('\\ \\\')",'');
    > ?>[/color]

    I would imagine you have the 2nd and 3rd arguments the wrong way round.




    --
    Oli

    Comment

    • yawnmoth

      #3
      Re: back slashes and preg_replace's e modifier


      Oli Filth wrote:[color=blue]
      > yawnmoth said the following on 05/11/2005 22:29:[color=green]
      > > Can anyone help me understand the following scripts output?:
      > >
      > > <?
      > > echo preg_replace('/.*/e',"sprintf('\\ \\\')",'');
      > > ?>[/color]
      >
      > I would imagine you have the 2nd and 3rd arguments the wrong way round.[/color]

      The order of the arguments is actually intentional. I'm trying to
      replace the empty string (the third argument) with that which the
      second argument would output when ran (as per the e modifier).

      The following demonstrates that replacement should be taking place:

      <?
      echo preg_replace('/.*/','replacement' ,'');
      ?>

      Comment

      Working...