php - Sort by part of filename

319

I have a$inhalte = glob("*.jpg"); listing pictures in a directory. Those files look like this

Constant1-Constant2-Input-ToBeSorted1-Date1.jpg
Constant1-Constant2-Output-ToBeSorted2-Date2.jpg
Constant1-Constant2-Input-ToBeSorted3-Date3.jpg
Constant1-Constant2-Output-ToBeSorted4-Date4.jpg
...

They should be sorted byToBeSorted alphabetically. Sorting them by name a/descendingly does not work, as theInput/Output interferes.

How do I accomplish that?

55

Answer

Solution:

strstr provides the string behind 'ToBeSorted' and helps to make a simple comparison for usort().

//$inhalte is the array supplied by glob()
usort($inhalte, function($a, $b){
  return strstr($a,"ToBeSorted") <=> strstr($b,"ToBeSorted");
});

People are also looking for solutions to the problem: javascript - Display alert message that's inside an action file, in another file

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.