With variable whitespaces as the delimiter between your two columns of text, there will be several ways to do this.
You could process the text file line-by-line with file()
and use preg_split()
to separate the text on variable spaces that are followed by a sequence of uppercase letters followed by the end of the string, or you could use file_get_contents()
with preg_match_all()
then extract the two captured columns with array_column()
. While the latter may be a little faster since it only makes 1 preg_
function call, the decision is likely to come down to the developer's coding tastes and the complexity of the input text.
Code: (Demo)
//$lines = file('your_text_file.txt', FILE_IGNORE_NEW_LINES);
$lines = [
'3M Company MMM',
'99 Cents Only Stores NDN',
'AO Smith Corporation AOS',
'Aaron\'s, Inc. AAN',
];
foreach ($lines as $line) {
[$names[], $symbols[]] = preg_split('~\s+(?=[A-Z]+$)~m', $line);
}
var_export($names);
echo "\n---\n";
var_export($symbols);
Or:
//$text = file_get_contents('your_text_file.txt');
$text = <<<TEXT
3M Company MMM
99 Cents Only Stores NDN
AO Smith Corporation AOS
Aaron's, Inc. AAN
TEXT;
preg_match_all('~(.+?)\s+([A-Z]+)$~m', $text, $matches, PREG_SET_ORDER);
var_export(array_column($matches, 1));
echo "\n---\n";
var_export(array_column($matches, 2));