字符串无用字符剥离-PHP

发布于 2024-08-25 01:05:39 字数 348 浏览 5 评论 0原文

我有一个大问题。我为我们网页上的东西做了一个特殊的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-09-01 01:05:39

您可以使用:

preg_replace("/^[^1-9]*(.*?)$/","$1",$str) as:

<?php

$arr = array('L0745','V1754', 'L0003', 'B0141', 'P0040', 'V8000');

foreach($arr as $str) {

    $str = preg_replace("/^[^1-9]*(.*?)$/","$1",$str);

    var_dump($str);
}

?>

输出:

C:\>php a.php
string(3) "745"
string(4) "1754"
string(1) "3"
string(3) "141"
string(2) "40"
string(4) "8000"

所用正则表达式的解释: ^[^1-9]*(.*?)$

  • ^ - 锚点开始匹配
    请求字符串。
  • $ - 锚点以匹配结尾
    细绳。
  • [1-9] - 单个非零数字
  • [^1-9] - 单个非 1-9 字符...可以
    包括 0 或任何其他字母。
  • .*? - 匹配其余的
  • () - 分组并记住...并在替换中使用。

这个正则表达式首先在字符串的开头传递非 1-9 个字符,然后匹配并记住其余的字符直到最后......并用记住的东西替换整个字符串。

You can use:

preg_replace("/^[^1-9]*(.*?)$/","$1",$str) as:

<?php

$arr = array('L0745','V1754', 'L0003', 'B0141', 'P0040', 'V8000');

foreach($arr as $str) {

    $str = preg_replace("/^[^1-9]*(.*?)$/","$1",$str);

    var_dump($str);
}

?>

Output:

C:\>php a.php
string(3) "745"
string(4) "1754"
string(1) "3"
string(3) "141"
string(2) "40"
string(4) "8000"

Explanation of the regex used: ^[^1-9]*(.*?)$

  • ^ - Anchor to start matching at the
    beg of the string.
  • $ - Anchor to match end of the
    string.
  • [1-9] - A single non-zero digit
  • [^1-9] - A single non 1-9 char...can
    include 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.

习ぎ惯性依靠 2024-09-01 01:05:39

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

缺⑴份安定 2024-09-01 01:05:39

在我看来,您希望数组的键和索引进行切换

尝试 array_flip(array)< /a>

$arr = array(
   'L0745' => 745, 
   'V1754' => 1754, 
   'L0003' => 3, 
   'B0141' => 141, 
   'P0040' => 40, 
   'V8000' => 8000
);

$new_arr = array_flip($arr);

foreach($new_arr as $k=>$v) {
    echo "Key: ".$k." Value: ".$v."<br />";
}

echo "<pre>".print_r(array_flip($arr),true)."</pre>";

Looks to me like you want the keys and the index of the array to switch

try array_flip(array)

$arr = array(
   'L0745' => 745, 
   'V1754' => 1754, 
   'L0003' => 3, 
   'B0141' => 141, 
   'P0040' => 40, 
   'V8000' => 8000
);

$new_arr = array_flip($arr);

foreach($new_arr as $k=>$v) {
    echo "Key: ".$k." Value: ".$v."<br />";
}

echo "<pre>".print_r(array_flip($arr),true)."</pre>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文