Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Array loop / string comparison help

Array loop / string comparison help

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpphphtmldatabasedata-structures
3 Posts 2 Posters 15 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bryant May
    wrote on last edited by
    #1

    Hi everyone, I'm relatively new to PHP and am experiencing a problem trying to loop through multiple arrays whilst comparing strings during each iteration of the loop. What I have is two arrays; one with a list of illegal file names and one with a list of filenames pulled from a scandir function. Although it doesnt work this is what im trying to do: $illegalArray = array(".php", ".html", ".sql"); $filesArray = array("general", "connect.php, "_sql", "test.sql"); foreach ($filesArray AS $f) // Loop through the file names array { foreach (illegalFiles AS $i) // Loop through the illegal file names array { if (strstr($f, $i) // Check to see if the the string held in $f contains any of the text held in $i print "illegal file"; else print "valid file"; } } Hopefully that kind of makes sense. Basically I want to loop through the files array, check to see if the file name contains the string held in the illegal array. If it does then do something, if it doesnt, do something else. I can get it working to the point where it outputs a list of the files that it shouldnt include (the illegal files) but I cant get it to output a list of files that it should include (from the $filesArray these would be the "general" and "_sql" file names). If anyone can shed any light on what I am doing wrong here I would be eternally grateful. Thanks for reading (sorry about the length) Bryant

    F 1 Reply Last reply
    0
    • B Bryant May

      Hi everyone, I'm relatively new to PHP and am experiencing a problem trying to loop through multiple arrays whilst comparing strings during each iteration of the loop. What I have is two arrays; one with a list of illegal file names and one with a list of filenames pulled from a scandir function. Although it doesnt work this is what im trying to do: $illegalArray = array(".php", ".html", ".sql"); $filesArray = array("general", "connect.php, "_sql", "test.sql"); foreach ($filesArray AS $f) // Loop through the file names array { foreach (illegalFiles AS $i) // Loop through the illegal file names array { if (strstr($f, $i) // Check to see if the the string held in $f contains any of the text held in $i print "illegal file"; else print "valid file"; } } Hopefully that kind of makes sense. Basically I want to loop through the files array, check to see if the file name contains the string held in the illegal array. If it does then do something, if it doesnt, do something else. I can get it working to the point where it outputs a list of the files that it shouldnt include (the illegal files) but I cant get it to output a list of files that it should include (from the $filesArray these would be the "general" and "_sql" file names). If anyone can shed any light on what I am doing wrong here I would be eternally grateful. Thanks for reading (sorry about the length) Bryant

      F Offline
      F Offline
      fly904
      wrote on last edited by
      #2

      What you had was fine, up until the point where you output the file each time it passed a validation check in the other array. eg. 'general' would output 'valid file' 3 times and 'connect.php' would output 'illegal file' once and then 'valid file' twice. What you should wait for is for all the checks to be done before outputing a result. The following snippet should work fine.

      $illegalArray = array(".php", ".html", ".sql");
      $filesArray = array("general", "connect.php", "_sql", "test.sql");

      foreach ($filesArray as $file)
      {
      $is_safe = true;
      foreach ($illegalArray as $illegal)
      {
      if(strstr($file, $illegal))
      $is_safe = false;
      }
      if ($is_safe)
      {
      echo 'Valid File ('.$file.')<br />';
      }
      else
      {
      echo 'Illegal File ('.$file.')<br />';
      }
      }

      Outputs:

      Valid File (general)
      Illegal File (connect.php)
      Valid File (_sql)
      Illegal File (test.sql)

      If at first you don't succeed, you're not Chuck Norris.

      B 1 Reply Last reply
      0
      • F fly904

        What you had was fine, up until the point where you output the file each time it passed a validation check in the other array. eg. 'general' would output 'valid file' 3 times and 'connect.php' would output 'illegal file' once and then 'valid file' twice. What you should wait for is for all the checks to be done before outputing a result. The following snippet should work fine.

        $illegalArray = array(".php", ".html", ".sql");
        $filesArray = array("general", "connect.php", "_sql", "test.sql");

        foreach ($filesArray as $file)
        {
        $is_safe = true;
        foreach ($illegalArray as $illegal)
        {
        if(strstr($file, $illegal))
        $is_safe = false;
        }
        if ($is_safe)
        {
        echo 'Valid File ('.$file.')<br />';
        }
        else
        {
        echo 'Illegal File ('.$file.')<br />';
        }
        }

        Outputs:

        Valid File (general)
        Illegal File (connect.php)
        Valid File (_sql)
        Illegal File (test.sql)

        If at first you don't succeed, you're not Chuck Norris.

        B Offline
        B Offline
        Bryant May
        wrote on last edited by
        #3

        Fly you're a legend. Thank you very very much for that, that has bugged me for almost 2 days (believe it or not). The beers are on me :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups