douhu5837 2014-01-09 07:50
浏览 27
已采纳

php setlocales第二个参数为字符串

I'm trying to use php setlocale() for dates. Setting it like:

setlocale(LC_TIME, 'pl_utf8','Polish','pl.UTF-8','Polish_Poland.1250','WINDOWS-1250');

works fine, but if I use variable which stores the second parameter like:

setlocale(LC_TIME, $locale);

it does not work.

Now var $locale contains the exact string 'pl_utf8','Polish','pl.UTF-8','Polish_Poland.1250','WINDOWS-1250' which is being pulled from database.

Why isn't working?

  • 写回答

2条回答 默认 最新

  • duanpi2033 2014-01-09 08:22
    关注
    $locale = "'foo','bar','baz'";
    setlocale(LC_TIME, $locale);
    

    is the same as

    setlocale(LC_TIME, "'foo','bar','baz'");
    

    which is not the same as

    setlocale(LC_TIME, 'foo', 'bar', 'baz');
    

    The latter passes 4 separate arguments, the former two versions just pass 2 arguments, with the second argument not being in a format that setlocale understands. Alternatively to passing multiple strings, you can also pass an array:

    setlocale(LC_TIME, array('foo', 'bar', 'baz'));
    

    But to get the string "'foo','bar','baz'" into the form of the array array('foo', 'bar', 'baz'), you have to do something like this:

    $locales = array_map(function ($l) { return trim($l, "'"); }, explode(',', $locale));
    

    Which is needlessly complicated. You need to store your locales differently in the database to simplify that, right now their format simply isn't suited well for this operation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试