PHP按字母顺序排序分隔文本文件然后填充下拉列表

时间:2023-01-16 19:55:52

I am having a few issues in doing something that I am sure is quite simple.

我在做一些我确信非常简单的事情时遇到了一些问题。

I have had a look for many hours on Google and * but I can't seem to find an answer. What I did find that was similar I tried to modify, but unfortunately without success.

我在谷歌和*上看了好几个小时,但我似乎无法找到答案。我发现的东西与我试图修改的相似,但遗憾的是没有成功。

I have a text file (names.txt) that contains values similar to this:

我有一个文本文件(names.txt),其中包含与此类似的值:

Jim|Bob|Keith|Fred|Steve<br>

I need a script that I can run every minute to change the content of the file to store the values in the same format but alphabetically:

我需要一个脚本,我可以每分钟运行一次,以更改文件的内容,以相同的格式存储值,但按字母顺序排列:

Bob|Fred|Jim|Keith|Steve<br>

then display them in an html dropdown box as options.

然后将它们显示在html下拉框中作为选项。

Any assistance would be very much appreciated. Thanks in advance for your assistance.

非常感谢任何帮助。提前感谢你的帮助。

1 个解决方案

#1


1  

You could try this:

你可以试试这个:

$fileName = 'names.txt';
$data     = file_get_contents($fileName);

// Assuming that the file had the data in one line...

// Split the data using the delimiter
$split = explode("|", $data);

// Sort
sort($split);

// Put it all back together
$data = implode("|", $split);

// Store it back in the file
file_put_contents($fileName, $data);

#1


1  

You could try this:

你可以试试这个:

$fileName = 'names.txt';
$data     = file_get_contents($fileName);

// Assuming that the file had the data in one line...

// Split the data using the delimiter
$split = explode("|", $data);

// Sort
sort($split);

// Put it all back together
$data = implode("|", $split);

// Store it back in the file
file_put_contents($fileName, $data);