Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2011, 11:29 AM   PM User | #1
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
does anyone know a function i could use instead of stristr

I have an array with the following elements

PHP Code:
define("LIST_OF_SCHOOLS""st patricks,Piarsaigh,Phiarsaigh,St Vincents,Glanmire,Farranree,North Presentation,north mon,north monestrey,Scoil iosagain,St aidans,knocknaheeny,mon"); 
PHP Code:
$schoolList explode(','LIST_OF_SCHOOLS); 
PHP Code:
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) {
    
            
normal($match[2]);
            for(
$cnt=0$cnt<count($schoolList); $cnt++) {
            
                
$school trim($schoolList[$cnt]);
                
                
green(stristr($match[2],$school));
                if(
$school!="" && stristr($match[2], $school)) { 
                
blue($match[2].": Match for school found");
                    unset(
$school);
                    return 
true;
                }            
            }    
    }
    else
    {
    
red("No school set on profile");
    }
    unset(
$school);
    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
kevinkhan is offline   Reply With Quote
Old 02-21-2011, 04:05 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
kevinkhan (02-21-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:32 PM.


Advertisement
Log in to turn off these ads.