Reading it again still puzzles me.

Let’s start by implementing tags that create boldface and italic text. Let’s say we want to begin bold text and [EB] to end bold text. Obviously, we must replace with <strong> and [EB] with </strong>.2 Achieving this is a simple application of eregi_replace:3

$joketext = eregi_replace('\', '<strong>', $joketext);
$joketext = eregi_replace('\[eb]', '</strong>', $joketext);

Notice that, because [ normally indicates the start of a set of acceptable characters in a regular expression, we put a backslash before it in order to remove its special meaning. As backslashes are also used to escape special characters in PHP strings, we must use a double backslash for each single backslash that we wish to have in the regular expression. Without a matching [, the ] loses its special meaning, so it doesn’t need to be escaped, although you could put a (double) backslash in front of it as well if you wanted to be thorough.



I get it that the [ must be escaped by a backslash, but why would double backslashes be necessary here? This should work the same:

$joketext = eregi_replace('[b]', '<strong>', $joketext);

The single backslash sees to it that is taken literally and not as a regex code only allowing 'b'. What am I missing?

    You are correct. In this case, single quote delimited strings are used, hence only one backslash is needed, unless that backslash comes at the end of the string or before a single quote.

    However, note that we should prefer preg_replace() to eregi_replace() as it may be faster and the ereg family of functions will be deprecated in PHP 6.

      So this on the other hand would be wrong?

      $joketext = eregi_replace("[b]", "<strong>", $joketext);

      Why?

        So this on the other hand would be wrong?

        It is not wrong, but the problem is that it looks like an unfamiliar escape sequence in a PHP string. Also, using double quoted strings for regex patterns complicates the regex pattern string when you have more complex patterns, precisely because the increased number of escape sequences means that more escaping needs to be done. With a single quoted string, only two escape sequences are recognised (\' and \), hence anything else is surely not an escape sequence.

          O, right, I already knew about using variables in single/double quotes:

          $var = 42
          echo 'My age is $var';
          // Outputs: My age is $var

          $var = 42
          echo "My age is $var";
          // Outputs: My age is 42

          Having read about strings and its extra escape sequences I now understand these outputs.

          echo 'My age is 42 \n Next year I will be 43';
          // Outputs:
          My age is 42 \n Next year I will be 43

          echo "My age is 42 \n Next year I will be 43";
          // Outputs:
          My ages is 42
          Next year I will be 43

          and if I would want to keep that \n I would have to use double escape backslashes when using double quoted strings:

          echo "My age is 42 \n Next year I will be 43";
          // Outputs:
          My age is 42 \n Next year I will be 43

          Right?

            laserlight wrote:

            You are correct. In this case, single quote delimited strings are used, hence only one backslash is needed, unless that backslash comes at the end of the string or before a single quote.

            But when using double quotes, I should use double backslashes? So it should be this?

            $joketext = eregi_replace("\", "<strong>", $joketext);

            Reading about it in:
            http://ctsasikumar.blogspot.com/2008/04/php-string.html
            I know that /[ would be an escape sequence in double quoted strings for a 'left bracket' character. So:
            $joketext = eregi_replace("[b]", "<strong>", $joketext);
            wouldn't that be literally anyway? (the [ would make the first [ literally a left bracket sign, making the entire sequence a normal string without special meaning...)

              But when using double quotes, I should use double backslashes? So it should be this?

              Yes. Note that this is a recommendation, not a requirement. You can use "[" and it would be equivalent to "\[", since "[" is not an escape sequence. The point is that the reader has to know that "[" is not an escape sequence, otherwise there would be doubt as to what it really means (and consequently the reader would have to interrupt his/her reading of your code to check the PHP manual).

              I know that /[ would be an escape sequence in double quoted strings for a 'left bracket' character.

              You mean "[" instead of "/[", but either way the article you read is incorrect. Check the PHP manual entry that I linked to for the correct list of escape sequences for double quoted strings.

              wouldn't that be literally anyway? (the [ would make the first [ literally a left bracket sign, making the entire sequence a normal string without special meaning...)


              Since "[" is not an escape sequence in PHP, it would result in two characters... which is the correct escape sequence for the regular expression pattern. But if you did not know or were misinformed that "[" was an escape sequence, you would then be in doubt and suspect that it was only "literally a left bracket", and thus there would be no escape sequence for the regular expression (which would be an incorrect conclusion). This is why I say that explicitly escaping backslashes in double quoted strings is good practice.

                Write a Reply...