parsing a string for keywords

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bill

    parsing a string for keywords

    I need to parse a set of directory entries (of photo image files)
    and pull out the names of the subjects, which is in the filename.

    A file name could contain 1 or more subject names from a limited
    set of 18 possibilities.

    eg: the possibilities might be: Aoife, Alana, Liam, Paddy.....

    the file name might be: 0709-Aoife-Alana-DSC12435.jpg

    I could do 18 stripos() calls to check for all 18 possibilities,
    but I thought I would run it by the more experienced to see if
    you can suggest an easier way

    thanks in advance.

    bill
  • Rik

    #2
    Re: parsing a string for keywords

    bill <nobody@spamcop .netwrote:
    I need to parse a set of directory entries (of photo image files) and
    pull out the names of the subjects, which is in the filename.
    >
    A file name could contain 1 or more subject names from a limited set of
    18 possibilities.
    >
    eg: the possibilities might be: Aoife, Alana, Liam, Paddy.....
    >
    the file name might be: 0709-Aoife-Alana-DSC12435.jpg
    >
    I could do 18 stripos() calls to check for all 18 possibilities, but I
    thought I would run it by the more experienced to see if you can suggest
    an easier way
    >
    thanks in advance.
    $possibities = array('foo','ba r','baz'....... .);
    $filenameparts = explode('-',$filename);
    $matches = array_intersect ($filenameparts ,$possibities);
    --
    Rik Wasmus

    Comment

    • bill

      #3
      Re: parsing a string for keywords

      Rik wrote:
      bill <nobody@spamcop .netwrote:
      >
      >I need to parse a set of directory entries (of photo image files) and
      >pull out the names of the subjects, which is in the filename.
      >>
      >A file name could contain 1 or more subject names from a limited set
      >of 18 possibilities.
      >>
      >eg: the possibilities might be: Aoife, Alana, Liam, Paddy.....
      >>
      >the file name might be: 0709-Aoife-Alana-DSC12435.jpg
      >>
      >I could do 18 stripos() calls to check for all 18 possibilities, but I
      >thought I would run it by the more experienced to see if you can
      >suggest an easier way
      >>
      >thanks in advance.
      >
      $possibities = array('foo','ba r','baz'....... .);
      $filenameparts = explode('-',$filename);
      $matches = array_intersect ($filenameparts ,$possibities);
      --Rik Wasmus
      absolutely lovely

      bill

      Comment

      • Rik

        #4
        Re: parsing a string for keywords

        bill <nobody@spamcop .netwrote:
        Rik wrote:
        >bill <nobody@spamcop .netwrote:
        >>
        >>I need to parse a set of directory entries (of photo image files) and
        >>pull out the names of the subjects, which is in the filename.
        >>>
        >>A file name could contain 1 or more subject names from a limited set
        >>of 18 possibilities.
        >>>
        >>eg: the possibilities might be: Aoife, Alana, Liam, Paddy.....
        >>>
        >>the file name might be: 0709-Aoife-Alana-DSC12435.jpg
        >>>
        >>I could do 18 stripos() calls to check for all 18 possibilities, butI
        >>thought I would run it by the more experienced to see if you can
        >>suggest an easier way
        >>>
        >>thanks in advance.
        > $possibities = array('foo','ba r','baz'....... .);
        >$filenamepar ts = explode('-',$filename);
        >$matches = array_intersect ($filenameparts ,$possibities);
        Addendum, if you want a case-insensitive comparison (PHP >= 5):
        $matches = array_uintersec t($filenamepart s,$possibities, 'strcasecmp');

        --
        Rik Wasmus

        Comment

        • bill

          #5
          Re: parsing a string for keywords

          Rik wrote:
          bill <nobody@spamcop .netwrote:
          >
          >Rik wrote:
          >>bill <nobody@spamcop .netwrote:
          >>>
          >>>I need to parse a set of directory entries (of photo image files)
          >>>and pull out the names of the subjects, which is in the filename.
          >>>>
          >>>A file name could contain 1 or more subject names from a limited set
          >>>of 18 possibilities.
          >>>>
          >>>eg: the possibilities might be: Aoife, Alana, Liam, Paddy.....
          >>>>
          >>>the file name might be: 0709-Aoife-Alana-DSC12435.jpg
          >>>>
          >>>I could do 18 stripos() calls to check for all 18 possibilities, but
          >>>I thought I would run it by the more experienced to see if you can
          >>>suggest an easier way
          >>>>
          >>>thanks in advance.
          >> $possibities = array('foo','ba r','baz'....... .);
          >>$filenamepart s = explode('-',$filename);
          >>$matches = array_intersect ($filenameparts ,$possibities);
          >
          Addendum, if you want a case-insensitive comparison (PHP >= 5):
          $matches = array_uintersec t($filenamepart s,$possibities, 'strcasecmp');
          >
          --Rik Wasmus
          thanks for the update, but my web host is still at 4.1.x

          bill

          Comment

          • Curtis

            #6
            Re: parsing a string for keywords

            On Jan 28, 12:28 pm, bill <nob...@spamcop .netwrote:
            Rik wrote:
            bill <nob...@spamcop .netwrote:
            >
            Rik wrote:
            >bill <nob...@spamcop .netwrote:
            >
            >>I need to parse a set of directory entries (of photo image files)
            >>and pull out the names of the subjects, which is in the filename.
            >
            >>A file name could contain 1 or more subject names from a limited set
            >>of 18 possibilities.
            >
            >>eg: the possibilities might be: Aoife, Alana, Liam, Paddy.....
            >
            >>the file name might be: 0709-Aoife-Alana-DSC12435.jpg
            >
            >>I could do 18 stripos() calls to check for all 18 possibilities, but
            >>I thought I would run it by the more experienced to see if you can
            >>suggest an easier way
            >
            >>thanks in advance.
            > $possibities = array('foo','ba r','baz'....... .);
            >$filenamepar ts = explode('-',$filename);
            >$matches = array_intersect ($filenameparts ,$possibities);
            >
            Addendum, if you want a case-insensitive comparison (PHP >= 5):
            $matches = array_uintersec t($filenamepart s,$possibities, 'strcasecmp');
            >
            --Rik Wasmus
            >
            thanks for the update, but my web host is still at 4.1.x
            >
            bill
            You could iterate through your arrays, changing the case of each
            element to lower/higher, and then compare with array_intersect . If you
            just put strtolower() around the second argument of explode, that
            should take care of that array. You could use array_map for the
            former.

            --
            Curtis

            Comment

            Working...