如何 preg_match 所有数字而不是 url 编码的逗号
How to preg_match all the digits and not the url coded commar
我下面URLhttps://test.example.com/blah/12345%2C80%2C8263473%2C9475834343
在 URL 的 https://test.example.com/blah/
部分之后,有数字被 %2C
分隔。我想获得 URL 中所有数字的数组。该数字可能从正 1 一直到正无穷大。 URL 中可能有无限数量的数字或根本没有数字(在这种情况下数组为空)。
在这种情况下,我想要一个像这样的数组:
[12345, 80, 8263473, 9475834343]
$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";
preg_match('/\b\d+\b/', $url, $matches);
print_r($matches);
然而,我的数组中唯一的元素是 12345
考虑仅拆分编码字符,以生成所需数字的数组
$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";
$url = preg_replace("/^.*\//", "", $url);
$matches = preg_split("/%\w{2}/", $url);
print_r($matches);
这会打印:
Array
(
[0] => 12345
[1] => 80
[2] => 8263473
[3] => 9475834343
)
您还可以使用 \G
锚点来获取这些数字:
(?:https?://\S+?/blah/|\G(?!^))(?:%2C)?\K\d+
说明
(?:
非捕获组
https?://\S+?/blah/
匹配协议,直到第一次出现 /blah/
|
或
\G(?!^)
在上一场比赛结束时断言当前位置,而不是开始
)
关闭非捕获组
(?:%2C)?
可选匹配 %2C
\K\d+
忘记目前匹配的是什么,匹配1+位
例子
$re = '`(?:https?://\S+?/blah/|\G(?!^))(?:%2C)?\K\d+`m';
$str = 'https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
输出
Array
(
[0] => 12345
[1] => 80
[2] => 8263473
[3] => 9475834343
)
不确定这在工作环境中如何与 PhP 一起使用,但您可以选择以下任一选项吗?
%2C|(?!.*\/)(\d+)
首先匹配“%2C”部分并使用交替匹配最后一个正斜杠后的数字组以获取数组中的这些数字组。同样,我不确定您如何从第一个捕获组中获取数组。
如果这样的事情会失败,也许可以使用回溯控制动词 (*SKIP)(*F)
从最终数组中排除“%2C”部分,如下所示:
%2C(*SKIP)(*F)|(?!.*\/)\d+
另一种可能的写法是:
(?:^.*\/|%2C)(*SKIP)(*F)|\d+
另一种解决方案:
$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";
$decodedUrl = urldecode($url); // convert url to .../blah/12345,80
$queryListString = substr($decodedUrl, strrpos($decodedUrl, "/") + 1); // get string after last slash
$intList = explode(',', $queryListString); // convert string to array by comma
这段代码非常简单,您可以使用它。首先,您需要制作 urldecode
,将其转换为常规字符串。然后再做一次操作。
我下面URLhttps://test.example.com/blah/12345%2C80%2C8263473%2C9475834343
在 URL 的 https://test.example.com/blah/
部分之后,有数字被 %2C
分隔。我想获得 URL 中所有数字的数组。该数字可能从正 1 一直到正无穷大。 URL 中可能有无限数量的数字或根本没有数字(在这种情况下数组为空)。
在这种情况下,我想要一个像这样的数组:
[12345, 80, 8263473, 9475834343]
$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";
preg_match('/\b\d+\b/', $url, $matches);
print_r($matches);
然而,我的数组中唯一的元素是 12345
考虑仅拆分编码字符,以生成所需数字的数组
$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";
$url = preg_replace("/^.*\//", "", $url);
$matches = preg_split("/%\w{2}/", $url);
print_r($matches);
这会打印:
Array
(
[0] => 12345
[1] => 80
[2] => 8263473
[3] => 9475834343
)
您还可以使用 \G
锚点来获取这些数字:
(?:https?://\S+?/blah/|\G(?!^))(?:%2C)?\K\d+
说明
(?:
非捕获组https?://\S+?/blah/
匹配协议,直到第一次出现/blah/
|
或\G(?!^)
在上一场比赛结束时断言当前位置,而不是开始
)
关闭非捕获组(?:%2C)?
可选匹配%2C
\K\d+
忘记目前匹配的是什么,匹配1+位
例子
$re = '`(?:https?://\S+?/blah/|\G(?!^))(?:%2C)?\K\d+`m';
$str = 'https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
输出
Array
(
[0] => 12345
[1] => 80
[2] => 8263473
[3] => 9475834343
)
不确定这在工作环境中如何与 PhP 一起使用,但您可以选择以下任一选项吗?
%2C|(?!.*\/)(\d+)
首先匹配“%2C”部分并使用交替匹配最后一个正斜杠后的数字组以获取数组中的这些数字组。同样,我不确定您如何从第一个捕获组中获取数组。
如果这样的事情会失败,也许可以使用回溯控制动词 (*SKIP)(*F)
从最终数组中排除“%2C”部分,如下所示:
%2C(*SKIP)(*F)|(?!.*\/)\d+
另一种可能的写法是:
(?:^.*\/|%2C)(*SKIP)(*F)|\d+
另一种解决方案:
$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";
$decodedUrl = urldecode($url); // convert url to .../blah/12345,80
$queryListString = substr($decodedUrl, strrpos($decodedUrl, "/") + 1); // get string after last slash
$intList = explode(',', $queryListString); // convert string to array by comma
这段代码非常简单,您可以使用它。首先,您需要制作 urldecode
,将其转换为常规字符串。然后再做一次操作。