Jump to content

[SOLVED] Searching for multiple possible substrings within a string


quischen

Recommended Posts

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.";
  }

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";
  }
  ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.