PHP 字符串函数在数字转换为字符串时无法按预期工作
我有以下代码,在邮政编码中放置一个空格。
(string)$postcode = $row['postcode'];
$first = substr($postcode, strlen($postcode)-3);
$second = substr($postcode, strlen($first));
$postcode = $first . ' ' . $second;
除了某些代码之外,该代码对于大多数代码都可以正常工作。即
PN45HA 70448
我不明白为什么?任何人都可以阐明这一点吗?
谢谢
I have the following code which places a space in postcode.
(string)$postcode = $row['postcode'];
$first = substr($postcode, strlen($postcode)-3);
$second = substr($postcode, strlen($first));
$postcode = $first . ' ' . $second;
The code works fine for most codes, except some. ie
PN45HA
70448
And i can't understand why? Can anyone shine a light on this?
Thankyou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不太明白这个问题,但如果是关于空间的,这会起作用:
Don't really understand the question, but if it's about the space, this will work:
问题是并非所有邮政编码都是简单的 6 字符邮政编码。您只需在传入的任何字符串的最后 3 个字符之前添加空格即可。
您可能需要做的是使用正则表达式:
这将采用任何看起来像邮政编码的字符串,两半之间有可选的空格,并放入一个空格中。
The problem is that not all of your postcodes are simple 6 character postcodes. You're simply adding space before the last 3 characters in whatever string you happen to pass in.
What you may need to do is use a regex:
This'll take any string which looks like a postcode, with optional spaces between the two halves, and puts in a single space.