简单的 substr 和 strlen 问题......我快疯了
好的,看一下这段代码:
$accountMask = substr($transaction['cc_number'], strlen($transaction['cc_number'])-4);
我认为它需要这样的数字:1234567890 并将其变成这样: 123456
我疯狂地认为代码实际上是这样做的: 7890 ?
在我看来,如果我只想显示某个数字(如 CC 号码)的最后 4 位,我会这样做:
$accountMask = substr($transaction['cc_number'], -4);
对吗?
Ok, so looking at this code:
$accountMask = substr($transaction['cc_number'], strlen($transaction['cc_number'])-4);
I think it takes a number like this: 1234567890
And turns it into this: 123456
Am I crazy to think that the code is actually doing this: 7890 ??
In my opinion, if I wanted to show just the last 4 digits of a number like a CC number, I would do this:
$accountMask = substr($transaction['cc_number'], -4);
Right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的两个例子都做了同样的事情。根据评论,请查看 substr() 的文档,
特别是第二个参数:
Both of your examples do the same thing. As per the comments check out the documentation on substr()
Particularly the second parameter:
要获得您想要的内容,请指定 $start=0,即使用:
或者:
您可以在此处阅读有关
substr()
的更多信息: http://php.net/manual/en/function.substr.phpTo get what you want, specify $start=0, i.e. use:
Or:
You could read more about
substr()
here: http://php.net/manual/en/function.substr.phpsubstr()
的定义:string substr ( string $string , int $start [, int $length ] )
您的代码将
$accountMask
设置为$transaction['cc_number']
的最后 4 个数字Definition of
substr()
:string substr ( string $string , int $start [, int $length ] )
Your code is setting
$accountMask
to the last 4 numbers of$transaction['cc_number']
substr
函数的签名位于此处。
因此,您要传递字符串和起始位置。之后就需要一切了。如果你想让它做你所说的那样,你会想这样称呼它:
The signature for the
substr
function isper here.
So you're passing in the string and the position to start at. It takes everything after that. If you wanted it to do what you said it looks like, you'd want to call it like this: