How do I use preg_match for partial matches?
When I have the string:
www.stackoverflow.com
and when I enter
stackoverflow.com or stackoverflow
to match the phrase it returns false. How do I make it work for partial matches?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
preg_matchuses regular expressions, which you can get a very good tutorial for here.In your specific case, you are looking for a string, followed by an optional segment (to change your search into English, you want to find "stackoverflow", optionally followed by ".com"). Sections of regular expressions can be made optional by using the
?modifier:The regular expression here (
/stackoverflow(\.com)?/) says the same thing as our English version above — match "stackoverflow" followed by an optional ".com".Here’s a quick overview of how the regex works:
stackoverflowmatches "stackoverflow"?. Without the group, only the token directly before the?would be made optional (in this case the "m").