Looking for preg_match_all
to extract any username found in a text. Example text is 'last_name, first_name Username:bjorge, Username philip kardashian, kim username: mercury, freddie'
What I'm trying to capture in this text:
-bjorge
-philip
-mercury
I tried this but no go: preg_match_all("/(Username:|Username)\s+?(\S+)/i", $input_lines, $output_array);
This is the output I get from what I tried.
array(30=>last_name, first_name 1=>last_name 2=>first_name) array(30=>bjorge, philip 1=>bjorge 2=>philip) array(30=>kardashian, kim 1=>kardashian 2=>kim) array(30=>mercury, freddie 1=>mercury 2=>freddie)
In the string 'last_name, first_name Username:bjorge, Username philip kardashian, kim username: mercury, freddie'
, there're no spaces before bjorge
.
Use:
preg_match_all("/(Username:|Username)\s*(\S+)/i", $input_lines, $output_array);
// here __^
I've simplified your regex:
$in = 'last_name, first_name Username:bjorge, Username philip kardashian, kim username: mercury, freddie';
preg_match_all("/Username:?\s*(\S+)/i", $in, $out);
print_r($out);
Output:
Array
(
[0] => Array
(
[0] => Username:bjorge,
[1] => Username philip
[2] => username: mercury,
)
[1] => Array
(
[0] => bjorge,
[1] => philip
[2] => mercury,
)
)