0

I've made this before, but I've forgotten all of the steps. I've figured how to grab the page using the file_get_contents() function and stripped out all of the unnecessary

$data = file_get_contents("index.php"); //read the file
$data = strip_tags($data);
$data = strtoupper($data);

Next, I'm using a custom explode function which removes all of the specified separates

$sep = "   ():.,!@#$%^&*[]{}?<>;";
$convert = superExplode($data, $sep);
function superExplode($str, $sep) {
    $i = 0;
    $arr[$i++] = strtok($str, $sep);
    while($token = strtok($sep))
    $arr[$i++] = $token;
    return $arr;
}

And finally, I count each instance of each word using array_count_values() which stores each word as a key and word count as the value

$count = array_count_values($convert);

Now I can simply use a foreach loop to get the key and the word count to store in the database. However, the problem I'm having is that, I'm getting blank keys in the $count array when I do a print_r($count). So for example:

print_r($count);

returns:

 Array ([] => 1
 [] => 2 
 [] => 1 
 [HOME] => 1 
 [] => 1 
 [SUBMIT] => 1 
 [NEW] => 1 
 [VIEW] => 1)

How can I filter out the keys that have nothing in them? Thanks.

Erica
  • 3
  • 1

3 Answers3

0

Use array_filter with no arguments to remove empty elements.

$convert = array_filter($convert);
$count = array_count_values($convert);
Xeoncross
  • 55,620
  • 80
  • 262
  • 364
  • Xeoncross, I just tried that, and it is not filtering out those blank keys. Any other ideas? – Erica May 06 '13 at 19:52
  • @Erica, I just realized that what you posed isn't possible (array_filter is still the correct way to filter empty elements but not *keys*). You have an error in your output since array's can only contain a *single* key of the same value. You can't have multiple array keys that are blank. Check your code. – Xeoncross May 06 '13 at 21:15
0

You can replace white spaces with a regular expression. I believe this would work...

$newData = preg_replace("/\s/", '', $data);

chuckieDub
  • 1,767
  • 9
  • 27
  • 46
  • chuckieDub, that is meshing all of the words together, but it did remove the white space. Still not what I need however. – Erica May 06 '13 at 19:56
  • Ah i see. You an loop through the elements of your array and delete them as in this post... http://stackoverflow.com/questions/369602/how-to-delete-an-element-from-an-array-in-php – chuckieDub May 06 '13 at 20:00
  • chuckieDub, thanks that does seem to be the best approach, however it's not recognizing the blank spaces as null nor " " just a blank space. – Erica May 06 '13 at 20:21
  • chuckieDub, thanks for your help. I did figure out the preg_replace it was '$data= preg_replace('/\s\s+/', ' ', $data);' instead – Erica May 06 '13 at 20:28
0

I would use the unset function in the foreach.

$array = array(
        '' => '1',
        '' => '2',
        '' => '1',
        'HOME' => '1',
        '' => '1',
        'SUBMIT' => '1',
        'NEW' => '1',
        'VIEW' => '1'
    );

function cleanArray($array){        
    foreach($array as $cleanMe => $value){
        if(empty($cleanMe)){
            unset($array[$cleanMe]);
        }
    }       
    return $array;
}

to test it

$arr = cleanArray($array);
print_r($arr);
unasAquila
  • 148
  • 9