unidox Posted January 19, 2009 Share Posted January 19, 2009 I am trying to use if preg_match but I am not to good at regex. I was wondering how I can do this: I have a form and the name of an input is username_required, I am trying to check to see if _required is in the name. How do I do that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/141526-search-string/ Share on other sites More sharing options...
DarkWater Posted January 19, 2009 Share Posted January 19, 2009 You don't need regex for this. <?php $field_name = "username_required"; if (strpos($field_name, "_required") !== FALSE) { //it's there } else { //nope } Quote Link to comment https://forums.phpfreaks.com/topic/141526-search-string/#findComment-740796 Share on other sites More sharing options...
MadTechie Posted January 20, 2009 Share Posted January 20, 2009 DarkWater's is better as it uses less resource but.. if you wanted a regex to check if it ended with "_required" then the below would work if (preg_match('/_required$/i', $field_name)) { //it's there } Quote Link to comment https://forums.phpfreaks.com/topic/141526-search-string/#findComment-740817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.