我正试图从用户获取句子并存储它们以便稍后进行比较,但我希望它们是字母顺序的,如;
Apple
Banana
取自像"Banana, Oranges And Apples"这样的字符串;在此之前,我先删掉了我不想要的词但我不知道该如何排序因为PHP sort()似乎只对数组
有效 <?php
$str = 'Man Methord Wifi HOlla Teddy Husband';
$result = trim( preg_replace(
"/[^a-z0-9']+([a-z0-9']{1,5}[^a-z0-9']+)*/i",
" ",
" $str "
) );
$lower_str = strtolower ($result);
echo $lower_str;
?>
这个函数可能适合你:
function sortstring($string,$unique = false) {
$string = str_replace('.', '', $string);
$array = explode(' ',strtolower($string));
if ($unique) $array = array_unique($array);
sort($array);
return implode(' ',$array);
}
您可以在http://www.wordinn.com/solution/226/php-sort-words-sentences
它有一个简单的函数,称为sort()
。在排序函数中,有许多不同类型的排序算法工作在排序函数上。这个函数有两个参数。排序函数的第一个参数是要排序的数组,第二个参数是排序算法的类型。
<?php
$data = array("a","k","b","z","i","v");
sort($data,SORT_STRING);
print_r($data);
?>