Multiline regular expression problem in PHP 5.3.3

Recently I came across a very strange behaviour in a regular expression I wrote years ago.

The webserver upgraded to PHP 5.3.3 and it suddenly stopped working.

Case as follows: I use PHP to read a text-file and with the aid of PCRE (preg_match & preg_split) I look for certain matches in that text-file. The program splits different paragraphs into seperate records. But suddenly preg_split stopped working:

<?php
$paragraphs = preg_split("/\n\n/",$contents_of_file);
?>

Multiple newlines could not be detected, whereas

<?php
$lines = preg_split("/\n/",$contents_of_file);
?>

generated no problem at all. At first I thought I had to use the multiline modifier /m but that didn’t work out either.

Although a HEX editor didn’t indicate any carriage returns in the text-file I managed to get things working by doing this:

<?php
$paragrapgs = preg_split("/\r?\n\r?\n/",$contents_of_file);
?>

Curious thing: previous and newer versions of PHP (e.g. 5.3.8 in a XAMPP test environment) did not require to look for possible occurrences of carriage returns (\r).

Leave a Reply

Your email address will not be published. Required fields are marked *