View Full Version : does anyone know a function i could use instead of stristr

02-21-2011, 12:29 PM
I have an array with the following elements

define("LIST_OF_SCHOOLS", "st patricks,Piarsaigh,Phiarsaigh,St Vincents,Glanmire,Farranree,North Presentation,north mon,north monestrey,Scoil iosagain,St aidans,knocknaheeny,mon");

$schoolList = explode(',', LIST_OF_SCHOOLS);

function checkForSchool($page, $schoolList) {
preg_match('%(Studied at|Went to|Goes to) \\\u003ca href=\\\\"http:\\\/\\\/www.facebook.com\\\/pages\\\/[a-zA-Z-]*\\\/\d*\\\\" data-hovercard=\\\\"\\\/ajax\\\/hovercard\\\/page.php\?[a-zA-Z=0-9]*\\\\">([a-zA-Z\s]*)\\\u003c\\\/a>%', $page, $match);
if($match && count($match)>0) {

for($cnt=0; $cnt<count($schoolList); $cnt++) {

$school = trim($schoolList[$cnt]);

if($school!="" && stristr($match[2], $school)) {
blue($match[2].": Match for school found");
return true;
red("No school set on profile");
return false;

How come my function is returning true for this match

mond Community High School
Inveralmond Community High School: Match for school found

I only want it too return true if all the match corresponds with one of the element in the array $schoolList

02-21-2011, 05:05 PM
You can use an in_array to make a comparison: in_array($match[2], $schoolList), just throw it in a branch condition. Don't need to iterate it or anything.
I don't see any purpose of your define though, the string isn't exactly ideal to work with. Just a simple array declaration would make a lot more sense, or you can read it in from a text file or something to make it easier using fgetcsv or something like that.

To answer your question, your last case is 'mon'. That matches Inveralmonnd, which is why you get the result. If you are looking for an identical comparison, you need to use str[case]cmp instead (for which you will need to pay attention to datatypes for the return).
If you are looking for things that *begin* with these list of schools, use the strpos function instead and ensure it returns identical to 0 (=== 0) in order to find it as the first needle in the string.