quischen Posted January 2, 2008 Share Posted January 2, 2008 Hi, I am attempting to use PHP to search for a list of possible values that must be entered from a text box on a sign up form. However, the user is still allowed to enter other text, as long as he or she also enters one of the predetermined values that I am searching their input for. If the user's input doesn't contain one of the predetermined value, an error message is returned. However, it doesn't seem to work with the multiple "Or" statements. Is there a better way of doing this? I've searched all over the web and on here and I'm still stuck. Here is what I have so far: $string = "Northwestern School Corporation"; //should trigger as true below $newString = ereg_replace("[^A-Za-z0-9]", "", strtolower($string)); //strips spaces out echo $newString."<br />"; //Supposed to match any of the following as true and return the error message. if ( (stristr($newString, 'elementary') === false) || (stristr($newString, 'school') === false) || (stristr($newString, 'middle') === false) || (stristr($newString, 'high') === false) || (stristr($newString, 'college') === false) || (stristr($newString, 'university') === false) ) { echo "Must contain one of the following: Elementary, School, Middle School, High School, College, or University"; } else { //Valid School echo "Valid school."; } Quote Link to comment https://forums.phpfreaks.com/topic/84192-solved-searching-for-multiple-possible-substrings-within-a-string/ Share on other sites More sharing options...
mrdamien Posted January 2, 2008 Share Posted January 2, 2008 stristr Returns all of haystack from the first occurrence of needle to the end. This means your stristr($newString, 'school') returns "schoolcorporation" and the test ("schoolcorporation" === false) is also false. I got it working simply by reversing the test: <? $string = "Northwestern School Corporation"; //should trigger as true below $newString = ereg_replace("[^A-Za-z0-9]", "", strtolower($string)); //strips spaces out echo $newString."<br />"; //Supposed to match any of the following as true and return the error message. if ( (stristr($newString, 'elementary') !== false) || (stristr($newString, 'school') !== false) || (stristr($newString, 'middle') !== false) || (stristr($newString, 'high') !== false) || (stristr($newString, 'college') !== false) || (stristr($newString, 'university') !== false) ) { //Valid School echo "Valid school."; } else { echo "Must contain one of the following: Elementary, School, Middle School, High School, College, or University"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84192-solved-searching-for-multiple-possible-substrings-within-a-string/#findComment-428720 Share on other sites More sharing options...
quischen Posted January 3, 2008 Author Share Posted January 3, 2008 Thank you, mrdamien. I wanted to do that myself but couldn't seem to figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/84192-solved-searching-for-multiple-possible-substrings-within-a-string/#findComment-428971 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.