PDA

View Full Version : More help with strings


JJR512
02-21-2002, 02:11 AM
Suppose I have a string. In the string is the pattern of one digit followed by one letter, like 1A. This pattern can occur 0 to 3 times. Each occurance will be different; in other words, the string could be something like AAA 1A 111 2B 332D C3 DD. I need to find any and ALL occurrances of it, and put each one into a different variable. For that example, I would need to find A1, 2B, and 3C. How do I do this?

Mark Hensler
02-21-2002, 05:41 AM
<?php

$text="AAA 1A 111 2B 332D 3C DD";

echo "<html><body><pre>\n";

$text = preg_match("#.+ ([0-9][A-z]) .+ ([0-9][A-z]) .+ ([0-9][A-z]) .+#", $text, $match);

foreach ($match as $key=>$val) {
echo "$key = $val\n";
}

echo "</pre></body></html>";

?>

Output:

0 = AAA 1A 111 2B 332D 3C DD
1 = 1A
2 = 2B
3 = 3C