Hi!

I wanted to search an array with the array_search function of PHP. Therefore I used the following code:

<?php
$array = file(".htpasswd01");
$index = array_search("Martin", $array);
echo $index;
?>

That didn't worked out.
Could you please tell me, how I have to do it right?

Thanks a lot!
Martin

P.S.: Sorry for my English, I'm from Germany...

    Well, I think the problem is, if that .htpasswd01 file is in the form of
    user:cryptpassword
    user1:cryptpassword

    then when you search for "Martin" it looks for just that but in the array the value is "Martin:xxxxxxxxx"

    If my theory is correct try the following code:

    <?php 
    $array = file(".htpasswd01"); // get the file in an array
    
    $tempArray = array();  //create an empty array for temp use
    
    foreach($array as $line) {  //iterate thru the array 1 line at a time
         $name = explode(":",$line);  //explode the line into 1 array
         array_push($tempArray, $line[0]); //push first element to temp
    }
    //now we have just the names in an array w/ corresponding keys
    
    $index = array_search("Martin",$tempArray); //search new array for the name
    
    if($index !== NULL) { //we found a match
         echo $array[$index]; //echo the value
    } else { //no match
         echo "Sorry, couldn't find Martin in the file";
    } 
    ?> 
    

    try that code, hopefully it will work, i havnt tested it.

      Hi drew010,

      thank you for the script, but it doesn't works...

      I always just get as answer:

      Martin:cryptedpassword

      If I search for an other username in the file, I also get Martin:cryptedpassword.

      CU,
      martin

        Do you mind posting the file that it is searching? Just replace the passwords with an x. and post any other code you are using to search if there is any.

          Ok, here are the files:

          .htpasswd01:
          Martin:[PASSWORD]
          Raine:[PASSWORD]
          phantomspawn:[PASSWORD]
          Sunshine:[PASSWORD]
          DirtyAngel:[PASSWORD]
          Dru:[PASSWORD]
          Fichtelmann:[PASSWORD]
          pccCMS_Support:[PASSWORD]

          Code I have written (works, but I have to enter Martin:[PASSWORD], I just want to enter Martin):

          <?php 
            function trimData(&$value)
            {
               $value = trim($value);
            }
          
            $array = file("meine_datei.txt"); 
          
            array_walk($array, "trimData");
          
            $ergebnis = array_search("Martin", $array); 
          
            if ($ergebnis === false)
              echo "Nothing found";
            else
              echo $ergbnis; 
          ?> 

          I hope, this is the stuff you wnated..

          CU,
          martin

            yes that is great. I am leaving right now for about 45 minutes to go get paid 🙂 for a coding job i did. ill come back and have a look. ill post my solution then.

              OK,

              thanks again a lot!

              Perhaps I won't answer today again, because in Germay it is now 9.00 o'clock in the evening, and tomorrow I have to go to school again :-)

              CU,
              martin

                have you tried [man]in_array[/man], that will give you a true or false on the test you are doing

                  in_array wont work either because it will search for "Martin" and a line in the array is Martin:password so it wont match. then it also wont return the key..

                  I fixed my code, I knew it should work I just put the wrong variable in place of something else. Here is the functioning code:

                  $search = "Fichtelmann"; //person to search for
                  
                  $array = file(".htpasswd01"); // get the file in an array 
                  
                  $tempArray = array();  //create an empty array for temp use 
                  
                  foreach($array as $line) {  //iterate thru the array 1 line at a time 
                       $name = explode(":",$line);  //explode the line into 1 array 
                       array_push($tempArray, $name[0]); //push first element to temp 
                  } 
                  //now we have just the names in an array w/ corresponding keys 
                  
                  $index = array_search($search,$tempArray); //search new array for the name 
                  
                  if($index === FALSE) { // no match
                     echo "Sorry, couldn't find $search in the file";   
                  } else { //found person echo $array[$index]; //echo the value
                  }

                  I tested it on my server and it works. Just put the username you are looking for in $search and run the code.

                    You might also want to look at [man]preg_grep[/man]. Have it search the array for the pattern "/Martin/" (or "/$whatever/".)

                      Write a Reply...