dpwtr666638 2014-11-07 09:49
浏览 62
已采纳

GET变量的Strlen

I have a strange bug in my site: I have a get variable in url ?getClass=9а For some reason when I output it's strlen it is on 1 symbol more than it actually is. For example:
9a strlen - 3
10a strlen - 4
11a strlen - 4
The other strange thing is that when I try to substring using it's strlen:

$classNumber=substr($_GET['getClass'],0,strlen($_GET['getClass']-1));
$classLetter=substr($_GET['getClass'], strlen($_GET['getClass']-1));

The result is like this: 9a $classNumber=9 $classLetter=а that's ok
10а $classNumer=1 $classLetter=0a that's wrong
11a $classNumber=11 $classLetter=a that's ok again. What's wrong with it?

  • 写回答

2条回答 默认 最新

  • doukuang6795 2014-11-07 10:18
    关注

    This correct answer was originally posted by user4035 but downvoted and deleted for some reason.


    The reason of this behaviour is that you are using a cyrillic "а", and not a latin one. And it is considered as a unicode character, represented with 2 bytes. You need to use mb_strlen function:

    <?php
    print strlen($_GET['getClass'])."<br>";
    print mb_strlen($_GET['getClass'], 'utf8');
    

    For input: "9а" it will print:

    3
    2
    

    But if you use plain ASCII, the functions will give the same result:

    getClass=99

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?