Forum Moderators: coopster
"Bartender wine for 3"
or
"Bartender beer for all!"
I have the allowed drinks stored in array
$menu = array("wine", "beer", "mustak");
and 3 is an id of the user - SELECT user FROM table WHERE id=3
The text returned should be:
"Here you are Sam, wine from Michal"
Michal is my name - easy to get
or
"Attention! Michal gives everybody beer"
My question is:
How can I accomplish that?
Moreover, if it's not a problem I would like to check if the text starts from "Bartender". If not, then just leave it (However that's easy with strpos)
Best regards
Michal Cibor
I tried
if(ereg("Bartender ([a-z]) for ([a-z0-9\!])", $text, $regs)) echo "Match found";
else echo "No match";
but it doesn't recognize the following text:
"Bartender wine for 3"
Where did I make a mistake?
Michal Cibor
$string = "Bartender wine for 3";
preg_match("/Bartender ([a-z]+) for (all\!¦[0-9]+)/Uis", $string, $names);
print '<pre>'; print_r($names); print '</pre>';
$string = "Bartender beer for all!";
preg_match("/Bartender ([a-z]+) for (all\!¦[0-9]+)/Uis", $string, $names);
print '<pre>'; print_r($names); print '</pre>';
The forum breaks the pipe symbol, you will have to rekey it if you copy and paste this code.
The two pattern modifiers [php.net] are also just carryovers. the "i" allows the pattern to match both upper and lower case letters and the "s" is just if you were using a dot metacharacter over a multiple line expression as it will also match newlines. Again, they are so common in most regexs I write that I simply forgot to pull them off in the example I posted.