Search code examples
phpspecial-characterspreg-split

$1 in subject string when using preg_split


I want to split a text into an array where the delimiter is one or more of "\n", and then put the content of the array as elements in a unorder html-list. When I do this using preg_split when there is a $1 in the subject string I get a weird result. Just looking at the array resulting from the splitting the result is fine and the $1 seems not to have caused any problem, but when I loop over the array and make it into a html list it creates a different result then expected(see example bellow)

Like if this was the subject string:

"First line

Second line $1

Third line"

It should become:

  • First line
  • Second line $1
  • Third line

But it becomes:

  • First line
  • Second lineFirst line Second line $1 Third line
  • Third line

Does anyone know why this happens? Is $1 some kind of special html or php character with a reserved meaning?

This is the code I've written:

        $listElements = preg_split('/[\n]+/',$subject);
        $output = '<ul>';
        foreach ( $listElements as $val ) {
          $output .= '<li>' . $val . '</li>';
        }
        $output .= '</ul>';

Solution

  • Using dollar symbols inside PHP strings has a weird effect to those who aren't familiar with it.

    $foo = "BAR";
    $example = "Hello, World! $foo $foo black sheep";
    echo $example;
    

    The above example echoes: Hello, World! BAR BAR black sheep. This is because the variable is expanded within the string.

    A simple way of stopping this behaviour is by using single quotes: Changing the above example to 'Hello, World! $foo $foo black sheep'; will echo this: Hello, World! $foo $foo black sheep, and the dollars will be left in place.

    Another way of stopping variables expanding inside double-quoted strings is by escaping them with a backslash:

    $example = "Hello, World! \$foo \$foo black sheep";
    

    will echo:

    Hello, World! $foo $foo black sheep


    In your example, I would assume that $val has a dollar symbol in it, which needs escaping.