1252748
1252748

Reputation: 15369

php replaces on every line even though multiline not specified

I extract the first line of a tab delineated text string using this:

$column_pattern = "/(.*?)\r\n/";
preg_match($pattern, $tsv, $columns);
print_r($columns[0]);

This successfully prints the first line of the text string. However, I would like to remove it so I have every line after this. However this

$rows = preg_replace($column_pattern, "", $tsv);
echo $rows;

replaces every row in the string. But I haven't used the multi line flag in the regex. Why would this behave like this?

Upvotes: 1

Views: 29

Answers (2)

anubhava
anubhava

Reputation: 785611

MULTiLINE doesn't mean what you think it does. MULTiLINE just means that line start/end anchors ^ and $ are matched for every line separated by newline characters.

preg_replace will apply the replacement string as many times as possible (match is found).

Upvotes: 1

Halcyon
Halcyon

Reputation: 57703

multiline means a match can span multiple lines, not that it will only look at the first line.

Upvotes: 1

Related Questions