字符串无用字符剥离-PHP
我有一个大问题。我为我们网页上的东西做了一个特殊的ID。让我们看一个例子:
H0059 - 这是一个特殊的 ID,称为注册号。最后两个字符是事物的 id。
我想剪掉无用的字符,以获得真实的 ID,这意味着删除第一个字符以及任何其他数字之前的所有 0。
示例:
L0745 => 745, V1754 => 1754, L0003 => 3, B0141 => 141, P0040 => 40, V8000 => 8000
请帮我解决这个问题。
我尝试过 strreplace 和爆炸但失败了:( 感谢您的帮助。
I've got a huge problem. I made a special ID for the things in our webpage. Let's see an example:
H0059 - this is the special ID called registration number. The last two chars are the things' id.
I'd like to cut off the useless characters, to get the real ID, what means strip the first char, and all the 0s before any other numbers.
Example:
L0745 => 745, V1754 => 1754, L0003 => 3, B0141 => 141, P0040 => 40, V8000 => 8000
Please help me in this.
I've tried with strreplace and explode but failed :( Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用:
输出:
所用正则表达式的解释:
^[^1-9]*(.*?)$
^
- 锚点开始匹配请求字符串。
$
- 锚点以匹配结尾细绳。
[1-9]
- 单个非零数字[^1-9]
- 单个非 1-9 字符...可以包括 0 或任何其他字母。
.*?
- 匹配其余的()
- 分组并记住...并在替换中使用。这个正则表达式首先在字符串的开头传递非 1-9 个字符,然后匹配并记住其余的字符直到最后......并用记住的东西替换整个字符串。
You can use:
Output:
Explanation of the regex used:
^[^1-9]*(.*?)$
^
- Anchor to start matching at thebeg of the string.
$
- Anchor to match end of thestring.
[1-9]
- A single non-zero digit[^1-9]
- A single non 1-9 char...caninclude 0 or any other alphabet.
.*?
- to match the rest()
- group and remember...and use in replacement.This regex first by passes non 1-9 char at the beg of the string and matches and remembers the rest till the end ...and replaces the whole string with the remembered thing.
ltrim(substr($input, 1), '0');
substr
从第一个字符开始字符串,跳过该字母。ltrim
将从前面修剪掉所有零。或者,如果您愿意:
preg_replace('/^.0*/', '', 'L0003'); //返回3
ltrim(substr($input, 1), '0');
substr
starts the string at the first character, skipping the letter.ltrim
will trim off all zeroes from the front.Or, if you prefer:
preg_replace('/^.0*/', '', 'L0003'); //returns 3
在我看来,您希望数组的键和索引进行切换
尝试 array_flip(array)< /a>
Looks to me like you want the keys and the index of the array to switch
try array_flip(array)