Reputation: 171
I'm trying to extract ID from a possibly huge text, what did I miss?
preg_match_all('/(ID\s\d+)/', "ID 20380843, ID 20675712", $matches);
print_r( $matches[0] );
Only return:
Array
(
[0] => ID 20380843
)
Instead of:
Array
(
[0] => ID 20380843
[1] => ID 20675712
)
Upvotes: 2
Views: 85
Reputation: 3097
Your problem isn't preg_replace_all, it's your source file. There's an invisible unicode character in the second ID - you can see by copy/pasting it into this Unicode Converter, you'll see U+200B show up in various forms in the lower boxes:
Unicode U+hex notation
preg_match_all('/(ID\s\d+)/', "ID 20380843, ID U+200B^20675712", $matches);
(emphasis mine)
This is the Unicode Zero-Width Spaaace, which is apparently not included in \s
as PHP's PREG defines it.
Upvotes: 2
Reputation: 16123
Did you copy that string from your code? Because there is something sneaky happening.
When I copied the code to my editor, it gave me this for string:
"ID 20380843, ID ?20675712"
As you can see, there is a questionmark-sign in the 2nd, thus failing your expression :)
Upvotes: 4
Reputation: 12040
print_r(matches)
instead of print_r(matches[0]);
try
preg_match_all('/(ID\s\d+)/', "ID 20380843, ID 20675712", $matches);
print_r( $matches );
Upvotes: 0