I would like to display the 'words' in alphabetical order, but all of the examples of solutions that I have come across, are not working for me.
I have tried using the implode() and explode() functions, using the file() function, and then using the sort() function.
Any help is very much appreciated, thank you!
<!DOCTYPE html>
<html>
<head>
<title>words list</title>
</head>
<body>
<h1>Word List</h1>
<?php
@$fp = fopen("words.txt", 'rb');
flock($fp, LOCK_SH); // lock file for reading
while (!feof($fp)) {
$words= fgets($fp);
explode($words, "\n");
file($words);
sort($words);
echo htmlspecialchars($words)."<br />";
}
flock($fp, LOCK_UN); // release read lock
fclose($fp);
?>
</body>
</html>
答案 0 :(得分:2)
<!DOCTYPE html>
<html>
<head>
<title>words list</title>
</head>
<body>
<h1>Word List</h1>
<?php
$lines = file("words.txt");
print_r($lines);
natsort($lines); // this will sort lines in your file
print_r($lines);
// This was optional here you can overwrite existing file or can create new file
file_put_contents("newtime.txt", implode("\n", $lines));
?>
</body>
</html>
我检查了一下,它对我有用! ! !
答案 1 :(得分:0)
Sorry; I misread your question. You need to put the values into an array in order to sort it.
> $i = 0;
> while (!feof($fp)) {
> $words[$i] = fgets($fp);
>
> $i++;
> }
> sort($words);
> echo htmlspecialchars($words)."<br />";