SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    extension from text in array

    I have this code:

    Code:
    function filter_file_extension_from_array($arg_array_files_list, $arg_filter_extension) {
    
          $extensions = explode(";", $arg_filter_extension);
          
          for ($g = 0; $g <= count($extensions) - 1; $g++) {
             echo $extensions[$g];
    
    
             for ($b = 0; $b <= count($arg_array_files_list) - 1; $b++) {
                if (substr($arg_array_files_list[$b], intval(strlen($arg_array_files_list[$b]) - 3)) == $extensions[$g]) {
                   echo " $g-ok";
                   $file_list[] = $arg_array_files_list[$b];
                }
             }
             
             
          }
          
          return $file_list;
          
       }
    please, can you tell me why having an array with some filenames, if I insert in $arg_filter_extension "mp3; flv", I obtain only filenames that have the first extension of the argument?
    where is the error?

    many thanks.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you want to inspect the value of something in php, I recomend you use var_dump() and not echo. var_dump() will tell you more. In particular, for a string, it not only quotes the value, giving you a better chance of noticing some potential whitespace, but it will tell you how many characters long it is, which is helpful because certain characters may not be visible.

    Try it, and see if you can figure your problem out.

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $arg_array_files_list is not scanned if $g==1 why?

  4. #4
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Missing some information I feel like, but here are some different ways of rewrite the problem.

    To answer your question why it may exit when $g==1 That would be because count($extensions) == 2

    PHP Code:
    <?php
        
    function filter_file_extension_from_array($arg_array_files_list$arg_filter_extension) {

        
    $extensions explode(";"$arg_filter_extension);
        foreach(
    $extensions as $extension) {
            print 
    $extension "<br>";
            
            foreach(
    $arg_array_files_list as $arg_file_list){
                if(
    substr($arg_file_listintval(strlen($arg_file_list)-3)) == $extension ){
                    
    $file_list[] = $arg_file_list;
                }
            }
        }
        
        
    /*Another way*/
        
        
    $extensions explode(";"$arg_filter_extension);
        foreach(
    $arg_array_files_list as $arg_file_list){
            
    $string substr($arg_file_listintval(strlen($arg_file_list)-3))
            if( 
    in_array($string$extensions) ){
                
    $file_list[] = $arg_file_list;
            } else {
                print 
    "$string doesn't exist in: ";
                
    var_dump($extensions);
            }
        }
    ?>

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    ok. many thanks for help me in the debug...
    actually the function is correct.
    it is important when you call this function, to write the list of the extensions without spaces.
    so this is wrong: "mp3; flv";
    while this is correct: "mp3;flv".

    many thanks.

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by franciska View Post
    it is important when you call this function, to write the list of the extensions without spaces.
    so this is wrong: "mp3; flv";
    while this is correct: "mp3;flv".
    You can use trim() to remove whitespace from the string
    PHP Code:
    if( in_array($stringtrim($extensions)) ){ 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Could just have the function return an array with only the files with the required extension.

    PHP Code:
    function filter_extentions $files$exts )
    {
        
    $exts joinpreg_split'/[;\s]+/'$exts, -1PREG_SPLIT_NO_EMPTY ), '|' );
        return 
    preg_grep"/\.(?:{$exts})$/i"$files );

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •