background image

14.

# )     

15.

natsort($items);     

16.

print_r($items);     

17.

# Outputs:     

18.

# Array     

19.

# (     

20.

# [2] => 5 apples     

21.

# [3] => 55 apples     

22.

# [0] => 100 apples     

23.

# [1] => 110 apples     

24.

# )   

  

9. levenshtein()

  

Levenshtein()告诉你两个单词之间的“距离”。它告诉你如果想把一个单词变成另一个

单词,需要插入、替换和删除多少字母。

  看个例子吧:

 

1.

$dictionary = array(     

2.

“php”, “javascript”, “css”     

3.

);     

4.

$word = “japhp”;     

5.

$best_match = $dictionary[0];     

6.

$match_value = levenshtein($dictionary[0], $word);     

7.

foreach($dictionary as $w) {     

8.

$value = levenshtein($word, $w);     

9.

if( $value < $match_value ) {     

10.

$best_match = $w;     

11.

$match_value = $value;     

12.

}     

13.

}     

14.

echo “Did you mean the ‘$best_match’ category?”;   

  

10. glob()

  

glob()会让你觉得用 opendir(), readdir()和 closedir()来寻找文件非常蠢。

 

1.

foreach (glob(“*.php”) as $file)     

2.

echo “$file\n”;