英文:
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
<?php
$str ="fix-str-123456789-yet-another-str fix-str-234";
$pattern = "/fix-str-\K\d+/";
preg_match($pattern, $str, $arr1); //Find only the first match.
echo "The first match: " . $arr1[0]; //Output: 123456789
echo "\n\n\n";
preg_match_all($pattern, $str, $arr2); //Find all the matches.
echo "All the matches: " . implode(',', $arr2[0]); //Output: 123456789,234
?>
答案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:
<?php
echo substr("Hello world",6);
?>
Info from here: https://www.w3schools.com/php/func_string_substr.asp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论