Page 1 of 1
matching pattern
Posted: Wed Aug 29, 2007 3:12 am
by johnhelen
Hello
I want to find matching pattern for variables: $word and $message as the following
if (preg_match("!^$word!", $message) {
....
}
By using this if $word is "student", messages that are mached can be:
1. student.
2. student name
3. studentname
4. student'
..
What I want to have only case 1 and two. That means ONLY "." or blank after word "student" is OK but not for all other cases
(here I donot count about upper or lower case)
How I can get this
Many thanks
john
Posted: Wed Aug 29, 2007 3:24 am
by stereofrog
If you want to match something, you have to add it to your pattern:
Thanks ...
Posted: Wed Aug 29, 2007 3:33 am
by johnhelen
Thanks
Posted: Wed Aug 29, 2007 4:40 pm
by johnhelen
Hello
If I use this
$pat = "/^$word[. ]$/";
With that pattern, I have an error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
And I also think that the word "student.", "student name" will match but "student" won't match...
Please help
Thanks
Posted: Wed Aug 29, 2007 4:55 pm
by stereofrog
Posted: Wed Aug 29, 2007 7:56 pm
by johnhelen
Thanks stereofrog
However, when I use this
"/^{$word}[. ]$/"
There is still error "Parse error: syntax error, unexpected T_RETURN in ..... mypage.php"
I play around with that code but no success
Regards
Posted: Thu Aug 30, 2007 3:29 am
by stereofrog
Please post the offending line and three-four lines above and below it.
Thanks
Posted: Thu Aug 30, 2007 6:12 pm
by johnhelen
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello
I am sorry, now there is no error.
However, only message "STOP." is matched not "STOP " or "STOP"
Here is the code:
=============================
Code: Select all
$words = array("STOP" , "CANCEL", "UNSUBSCRIBE");
foreach($words as $word) {
if (preg_match("/^{$word}[. ]$/", $message )) {
echo "Match value : ".$word;
}
}
===============================
Thank you for your help
Regards
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]