使用 PHP 修剪字符串开头的所有零 [英] Trim any zeros at the beginning of a string using PHP

查看:22
本文介绍了使用 PHP 修剪字符串开头的所有零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户将在一个字段中填写与其帐户相关的数字.不幸的是,一些用户会在号码的开头加上零来组成一个六位数的号码(例如 000123、001234),而其他用户则不会(例如 123、1234).我想修剪"前面带有零前缀的用户的数字,因此如果用户输入 000123,它将删除零成为 123.

Users will be filling a field in with numbers relating to their account. Unfortunately, some users will have zeroes prefixed to the beginning of the number to make up a six digit number (e.g. 000123, 001234) and others won't (e.g. 123, 1234). I want to 'trim' the numbers from users that have been prefixed with zeros in front so if a user enters 000123, it will remove the zeroes to become 123.

我已经看过了 trim 和 substr,但我不相信它们能完成这项工作?

I've had a look at trim and substr but I don't believe these will do the job?

推荐答案

您可以使用 ltrim() 并将应删除的字符作为第二个参数传递:

You can use ltrim() and pass the characters that should be removed as second parameter:

$input = ltrim($input, '0');
// 000123 -> 123

ltrim 仅从字符串的开头(左侧) 删除指定的字符(默认空格).

ltrim only removes the specified characters (default white space) from the beginning (left side) of the string.

这篇关于使用 PHP 修剪字符串开头的所有零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆