Because your pattern is ONLY matching the exact string literal "A-Z", and none of your strings contain that. If they did, they would match
$str =
'1. 1 Smith/Ada # 1 A EDSSZS
4. L 1 A-Z/qwery # 1 C ASDAS';
$arr = explode("\n", $str);
echo '<pre>' . print_r($arr, 1) . '</pre>';
$p = '[A-Z]';
foreach ($arr as $s){
# echo $s;
preg_match($p, $s, $match);
echo print_r($match,1 ) . '<br/>';
}
The reason is that you probably want to match the character class "[A-Z]" and failed to notice
http://se2.php.net/manual/en/regexp.reference.delimiters.php wrote:
In addition to the aforementioned delimiters, it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, respectively.
{this is a pattern}
What you want, for starters, is
$pattern = '#[A-Z]#';
But that pattern will match a single character from A to Z inclusive, and uppercase. Using your original example
1. 1 Smith/Ada # 1 A EDSSZS
2. L 1 Andrews/To# 2 B RDDDSA
3. L 1 Kerr/Gerry# 2 B GGFFDD
$match would contain 'S', 'A' and 'K' in each respective iteration. If you want to grab "Smith", "Andrews" and "Kerr", use
$pattern = '#\d+\s+\K[A-Za-z]+(?=/)#';
Which means:
Match 1 or more digits
followed by 1 or more whitespaces
reset the pattern matching as if it started at this point
match one or more characters from A to Z or a to z
that are followed by a slash
You could simplify it by dropping \d+\s+\K from the pattern and probably not match anything other than what you want anyway. But hard to say without knowing the exact specification for format of input data which is why I'm more specific here. For example, the line I threw into the string in my first code snippet, containing "A-Z/qwery", would then also match.