如何从字符串 “constant-string-NUMBER-*” 中获取数字 “NUMBER”?

huangapple go评论70阅读模式
英文:

How to get NUMBER in a string "constant-string-NUMBER-*"?

问题

我有像constant-string-NUMBER-*这样的字符串,其中

  • constant-string-是一个已知的常量字符串(我知道并可以在获取NUMBER的过程中使用),例如fix-str-
  • NUMBER是任何自然数
  • -*可以是任何字符串

字符串结果示例:

fix-str-0
// 结果: 0

fix-str-0-another-str
// 结果: 0

fix-str-123
// 结果: 123

fix-str-456789
// 结果: 456789

fix-str-123456789-yet-another-str
// 结果: 1234567899

fix-str-999999-another-str-123
// 结果: 999999

我想在PHP中从这些字符串中提取NUMBER,以便我可以将这个数字关联到一个变量,例如$numberFromString = ?

有什么建议吗?

英文:

I've strings like constant-string-NUMBER-* where

  • constant-string- is a costant string (that I know and can use in the effort of getting the NUMBER) e.g. fix-str-
  • NUMBER is any natural number
  • -* can be any string

String-result examples:

fix-str-0
// result: 0

fix-str-0-another-str
// result: 0

fix-str-123
// result: 123

fix-str-456789
// result: 456789

fix-str-123456789-yet-another-str
// result: 1234567899

fix-str-999999-another-str-123
// result: 999999

I would like to extract the NUMBER from those strings in PHP so that I can associate this number to a variable e.g. $numberFromString = ?.

Any insight?

答案1

得分: 2

使用您的字符串示例,有两种可能的方法。一种方法可以使用 explode,另一种方法可以使用 preg_match 进行正则表达式匹配。我将展示这两种方法,只是为了说明正则表达式并不总是绝对必要的。

使用 explode

$strings = [
    'fix-str-0',
    'fix-str-0-another-str',
    'fix-str-123',
    'fix-str-456789',
    'fix-str-123456789-yet-another-str',
    'fix-str-999999-another-str-123',
];

$match = [];
$matches = [];
foreach ($strings as $string) {
    $match = explode('-', $string);

    if (count($match) >= 3) {
        $matches[] = $match[2]; // 数组索引 2 包含数字
    }
}

foreach ($matches as $found) {
    echo $found, PHP_EOL;
}

// 输出:

0
0
123
456789
123456789
999999

使用 preg_match

$strings = [
    'fix-str-0',
    'fix-str-0-another-str',
    'fix-str-123',
    'fix-str-456789',
    'fix-str-123456789-yet-another-str',
    'fix-str-999999-another-str-123',
];

$match = [];
$matches = [];
foreach ($strings as $string) {
    // 匹配一个或多个数字,并存储到 $match 中
    preg_match('/(\d+)/', $string, $match);

    if (!empty($match)) {
        $matches[] = $match[0]; // 使用第一个匹配结果
    }
}

foreach ($matches as $found) {
    echo $found, PHP_EOL;
}

// 输出:

0
0
123
456789
123456789
999999
英文:

Based on your string examples, there are two possible ways. One way could use explode, then of course the other way could be with preg_match for regex. I'll show both ways, only to show that regex is not always absolutely necessary.

Using explode:

$strings = [
'fix-str-0',
'fix-str-0-another-str',
'fix-str-123',
'fix-str-456789',
'fix-str-123456789-yet-another-str',
'fix-str-999999-another-str-123',
];

$match = [];
$matches = [];
foreach ($strings as $string) {
    $match = explode('-', $string);
    
    if (count($match) >= 3) {
        $matches[] = $match[2]; // Array offset 2 has the number
    }
}

foreach($matches as $found) {
    echo $found, PHP_EOL;
}

// Output:

0
0
123
456789
123456789
999999

Using preg_match:

$strings = [
'fix-str-0',
'fix-str-0-another-str',
'fix-str-123',
'fix-str-456789',
'fix-str-123456789-yet-another-str',
'fix-str-999999-another-str-123',
];

$match = [];
$matches = [];
foreach ($strings as $string) {
      // match 1 or more digits, store to $match
    preg_match('/(\d+)/', $string, $match);
    
    if (!empty($match)) {
        $matches[] = $match[0]; // use the first match
    }
}

foreach($matches as $found) {
    echo $found, PHP_EOL;
}

// Output:

0
0
123
456789
123456789
999999

答案2

得分: 1

尝试这个:

fix-str-(\d+)


 - `fix-str-` 匹配这个字符串。
 - `(\d+)` 然后匹配一个或多个数字,并保存在第一个捕获组中。

**编辑**

从 @user3783243,我们还可以使用 `fix-str-\K\d+`,而不需要使用捕获组。

fix-str-\K\d+

 - `fix-str-` 匹配这个字符串,然后..
 - `\K` 重置报告匹配的起始点。
 - `\d+` 然后匹配一个或多个数字。

查看[正则表达式演示][1]

```php
<?php 
$str ="fix-str-123456789-yet-another-str fix-str-234";

$pattern = "/fix-str-\K\d+/";

preg_match($pattern, $str, $arr1); //仅查找第一个匹配项。

echo "第一个匹配项: " . $arr1[0]; //输出: 123456789

echo "\n\n\n";

preg_match_all($pattern, $str, $arr2); //查找所有匹配项。

echo "所有匹配项: " . implode(',', $arr2[0]); //输出: 123456789,234
?>


<details>
<summary>英文:</summary>

Try this:

fix-str-(\d+)


 - `fix-str-` match this string.
 - `(\d+)` followed by one or more digits, and save the number inside the first capturing group.

**EDIT**

From @user3783243, we can also use `fix-str-\K\d+`, without the need of a capturing group.

fix-str-\K\d+

 - `fix-str-` match this string, then..
 - `\K` reset the starting point of the reported match.
 - `\d+` then match one or more digits.


See [regex demo][1]

```php
&lt;?php 
$str =&quot;fix-str-123456789-yet-another-str fix-str-234&quot;;

$pattern = &quot;/fix-str-\K\d+/&quot;;

preg_match($pattern, $str, $arr1); //Find only the first match.

echo &quot;The first match: &quot; . $arr1[0]; //Output: 123456789

echo &quot;\n\n\n&quot;;

preg_match_all($pattern, $str, $arr2); //Find all the matches.

echo &quot;All the matches: &quot; . implode(&#39;,&#39;, $arr2[0]); //Output: 123456789,234
?&gt;

答案3

得分: -1

你可以将一个字符串表示为字符数组。使用 PHP 的 substr() 函数,其中第二个参数是你的字符串左侧的起始位置。

例如,从字符串中返回 "world":

<?php
echo substr("Hello world", 6);
?>

详细信息请查看此链接:https://www.w3schools.com/php/func_string_substr.asp

英文:

You can represent a string as an array of characters. Use the PHP substr() Function, where the second argument is the number from which your string is left.

Example. Return "world" from the string:

&lt;?php
echo substr(&quot;Hello world&quot;,6);
?&gt;

Info from here: https://www.w3schools.com/php/func_string_substr.asp

huangapple
  • 本文由 发表于 2023年1月9日 06:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定