SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finding needle, in an array, based upon kind

    I can't put my finger on exactly how to do this. I want to check $string and $kind against $search. For example, if $kind == movie and $string contains '(widescreen)', we would have a match. However, if $kind == 'tv show' and $string contained '(widescreen)' we would not have a match. If $string contained '[region 2]', it wouldn't matter what $kind was, as it's a catchall and we would have a match.

    PHP Code:
    $search = array();
    $search = (
        
    '(widescreen)' => 'movie',
        
    '[region 2]' => '',
        
    '(director\'s cut)' => 'movie',
        
    'season 1' => 'tv show'
    );

    // match
    $kind 'movie';
    $string 'The Dark Knight (widescreen)';

    // match
    $kind 'movie';
    $string "Meatballs (director's cut)";

    // match
    $kind 'tv show';
    $string 'Frasier - season 1';

    // no match
    $kind 'movie';
    $string 'The Dark Knight';

    // match
    $kind 'tv show';
    $string 'Batman [region 2]'
    Hopefully this makes sense!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Nope.

    How is the searched data contained? Is it within an array, file, database...?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This seems to meet your initial outline and the specifications supplied, is it of any use?

    PHP Code:
    <?php

    /**
     * @param Array $aSearchParams The search parameters
     * @param String $sTitle The item title
     * @param String $sKind The item kind
     * @return Boolean
     */
    function performSearch$aSearchParams$sTitle$sKind )
    {
        foreach (
    $aSearchParams as $sKindParam => $sTitleParam){
            if( (
    false !== strstr($sKind$sKindParam)) && (false !== strstr($sTitle$sTitleParam)) )
            {
                return 
    true;
            }
        }
        return 
    false;
    }

    $aSearchParams = array(
        
    '(widescreen)' => 'movie',
        
    '[region 2]' => '',
        
    '(director\'s cut)' => 'movie',
        
    'season 1' => 'tv show'
    );

    $sKind 'movie';
    $sTitle 'The Dark Knight';

    echo ( 
    performSearch($aSearchParams$sKind$sTitle) ) ? 'Match.' 'No Match.' ;

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Nope.

    How is the searched data contained? Is it within an array, file, database...?
    It's in a string.
    $string = $_POST['foo'];

    I want to parse that string to see if there's a match. if there's a match, then die('error message');

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not at neat but explains the logic

    PHP Code:
    $search = array( 
         
    'widescreen' => 'movie',
        
    '[region 2]' => '',
        
    '(directors cut)' => 'movie',
        
    'season 1' => 'tv show',
    );

    //match
    //$kind = 'movie';
    //$string = 'The Dark Knight (widescreen)';
    // match
    //$kind = 'movie';
    //$string = "Meatballs (directors cut)";
    // match
    //$kind = 'tv show';
    //$string = 'Frasier - season 1';
    // no match
    $kind 'movie';
    $string 'The Dark Knight';
    // match
    //$kind = 'tv show';
    //$string = 'Batman [region 2]'; 

    $match=0;

    foreach ( 
    $search as $k=>$v ){

     if( 
    $v == $kind && strpos$string $k ) ) {
      
    $match++;
     }elseif( 
    strpos$string'[region 2]')){
      
    $match++;
     }

    }

    if( 
    $match) echo 'Match ' $kind .'-' $string 

  6. #6
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys!

    SilverBulletUK, I think yours is the one that's gonna do it. However, I receive an error if trying to use the generic 'region 2'. prolly 'cause the field is empty.

    So, what about changing that empty field to 'any' and then telling the function if that shows up in *any* string, regardless of movie, tv show, whatever, it's a match?

    PHP Code:
    $aSearchParams = array(
        
    'region 2' => 'any',
    );
    // match
    $sKind 'TV Show';
    $sTitle 'Cops [region 2]';

    // match
    $sKind 'Movie';
    $sTitle 'Hulk [region 2]'
    it would probably be a good idea to make it case-insensitive as well. Which I was thinking of doing like this, unless there's a better way?

    PHP Code:
    $sKind strtolower('movie');
    $sTitle strtolower('The Dark Knight'); 
    Thanks again, this is sweet!

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What error are you getting? I don't seem to be getting any.

    Here's the revised case-insensitive function.
    PHP Code:
    /**
     * @param Array $aSearchParams The search parameters
     * @param String $sTitle The item title
     * @param String $sKind The item kind
     * @return Boolean
     */
    function performSearch$aSearchParams$sTitle$sKind )
    {
        foreach (
    $aSearchParams as $sKindParam => $sTitleParam){
            if( (
    false !== stristr($sKind$sKindParam)) && (false !== stristr($sTitle$sTitleParam)) )
            {
                return 
    true;
            }
        }
        return 
    false;

    [Edit] I've found the error, let me have a quick play.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Bug fixed.

    PHP Code:
    /**
     * @param Array $aSearchParams The search parameters
     * @param String $sTitle The item title
     * @param String $sKind The item kind
     * @return Boolean
     */
    function performSearch$aSearchParams$sTitle$sKind )
    {
        foreach (
    $aSearchParams as $sKindParam => $sTitleParam){
            if( 
    false !== stristr($sKind$sKindParam) )
            {
                if( (empty(
    $sTitleParam)) || (false !== stristr($sTitle$sTitleParam)) )
                {
                    return 
    true;
                }
            }
        }
        return 
    false;

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beautiful. Works like a charm!

    Thank you very much


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •