Reputation: 1073
I'm looking at a regular expression to match any characters. I know that the '.' is a placeholder except for newline. Given this code below:
$fruits = "One\nTwo\nThree";
preg_match_all('/^(.*)$/', $str, $matches);
print_r($matches);
Why does it not match anything at all? I would think, $matches[0] would be One Two Three?
Upvotes: 0
Views: 102
Reputation: 5351
Add the modifier "s" to the regex:
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
$fruits = "One\nTwo\nThree";
preg_match_all('/^(.*)$/s', $fruits, $matches);
print_r($matches);
Update:
If you enclose $fruits in single quotes, the newline isn't treated as such and the replacement also works, event without the "s" modifier. But I don't know if the output is what you expect it to be ;)
$fruits = 'One\nTwo\nThree';
preg_match_all('/^(.*)$/', $fruits, $matches);
print_r($matches);
Upvotes: 2