matching pattern
Moderator: General Moderators
matching pattern
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
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
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
If you want to match something, you have to add it to your pattern:
Code: Select all
$pat = "/^$word[. ]$/";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
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
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Code: Select all
"/^{$word}[. ]$/"- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Thanks
feyd | Please use
===============================
Thank you for your help
Regards
feyd | Please use
Code: Select all
,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
,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]