• ベストアンサー

先頭のTHE、A などを無視してソート

PHP+Mysqlでデータベースを利用しています。 質問タイトルの通り、 データベースの内容を表示する際、 ソートをするときに 先頭のTheやAといった文字列を無視してソートしたいのですが、 どのようにすればいいのでしょうか? 例: Apple The Bear Candle A Duck よろしくご教授下さい。

質問者が選んだベストアンサー

  • ベストアンサー
  • yambejp
  • ベストアンサー率51% (3827/7415)
回答No.1

usortとかどうでしょ? <? $words=Array("Apple","The Bear","Candle","A Duck"); shuffle($words); //シャッフル print "<pre>"; print_r($words); print "</pre>"; usort($words, "cmp"); //指定ソート print "<pre>"; print_r($words); print "</pre>"; function cmp($a, $b){ $a=preg_replace("/^a |^the /i","",$a); $b=preg_replace("/^a |^the /i","",$b); if ($a==$b){ return 0; } return ($a < $b) ? -1 : 1; } ?> http://www.php.net/manual/ja/function.usort.php

すると、全ての回答が全文表示されます。

関連するQ&A