Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. <?php
  2. $letters = range( 'a','z' );
  3. array_push( $letters, 'å', 'ä', 'ö' );
  4. foreach ( $letters as $index=>$letter ) : ?>
  5. <h3><?php echo $letter; ?></h3>
  6. <ul>
  7. <?php
  8. $tags = get_tags( array('name__like' => $letter, 'hide_empty' => 0) );
  9. foreach ( $tags as $tag ) :
  10. ?>
  11. <li><a href="/tag/<?php echo $tag->slug; ?>/"><?php echo $tag->name; ?></a></li>
  12. <?php endforeach; ?>
  13. </ul>
  14. <?php endforeach; ?>
  15.  
  16. function sort_nordic(&$array) {
  17. uasort($array, 'nordic_cmp');
  18. }
  19. function nordic_cmp($a, $b) {
  20. // If å, ä, and ö is missing from first string, just use PHP's native function
  21. if (preg_match('/([å]|[ä]|[ö]|[Å]|[Ä]|[Ö])/', $a) == 0) {
  22. return strcoll($a, $b);
  23. }
  24. // If å, ä, and ö is missing from second string, also use PHP's native function
  25. if (preg_match('/([å]|[ä]|[ö]|[Å]|[Ä]|[Ö])/', $b) == 0) {
  26. return strcoll($a, $b);
  27. }
  28.  
  29. // Arriving here both the strings contains some characters we have too look out for
  30. // First, create arrays from the strings so we can easily loop over the characters
  31. // Comparison is made in lowercase
  32. $a_arr = preg_split('//u', mb_strtolower($a), -1, PREG_SPLIT_NO_EMPTY);
  33. $b_arr = preg_split('//u', mb_strtolower($b), -1, PREG_SPLIT_NO_EMPTY);
  34.  
  35. // Get the length of the shortest string
  36. $end = min(mb_strlen($a), mb_strlen($b));
  37.  
  38. // Loop over the characters and compare them in pairs
  39. for ($i = 0; $i < $end; $i++) {
  40. // Check if character in the first string is one that we have to correct for
  41. if (mb_stripos("åäö", $a_arr[$i]) !== false) {
  42. // Computes the corrected return value. The first character "-" is just a
  43. // nonsene placeholder to return 1 for "ä", 2 for "å" and 3 for "ö"
  44. $r = mb_stripos("-åäö", $a_arr[$i]) - mb_stripos("-åäö", $b_arr[$i]);
  45. if ($r != 0) {
  46. return $r;
  47. }
  48. } else {
  49. // If the character is not a character that we have to correct for
  50. // the PHP native works fine
  51. $r = strcoll($a_arr[$i], $b_arr[$i]);
  52. if ($r != 0) {
  53. return $r;
  54. }
  55. }
  56. }
  57. // Fallback: If so far there has been no call to return() then the
  58. // strings are idenical up until the length of the shorter string.
  59. // Then let the lengths of the strings determine the order
  60. return mb_strlen($a) - mb_strlen($b);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement