在 URL 中获取正斜杠之间的变量
Getting variables between slash forwards in a URL
假设我有一个 URL:https://somesite.com/0/posts/20/290/755653-Title.html
我如何获得这些变量:/0/, /20/, /290/
?注意它们是变量,它们总是不同的。
我以为我可以这样得到它们:
$url = '//somesite.com/0/posts/20/290/755653-Title.html';
var_dump(parse_url($url));
但数组没有将它们显示为单独的变量。应该用 preg_replace
代替吗?我不认为我知道如何。感谢您的帮助。
您可以使用 explode() 并将字符串转换为由“/”分隔符分隔的数组。
<?php
// Example 1
$url = "https://somesite.com/0/posts/20/290/755653-Title.html";
$pieces = explode("/", $url);
echo $pieces[0] . "<br />";
echo $pieces[1] . "<br />";
echo $pieces[2] . "<br />";
echo $pieces[3] . "<br />";
echo $pieces[4] . "<br />";
echo $pieces[5] . "<br />";
echo $pieces[5] . "<br />";
echo $pieces[6] . "<br />";
echo $pieces[7] . "<br />";
echo "<hr />";
// Example 2
$data = "https://somesite.com/0/posts/20/290/755653-Title.html";
list($first, $second, $third, $fourth, $fifth, $sixth, $seventh, $eighth) = explode("/", $url);
echo $first . "<br />";
echo $second . "<br />";
echo $third . "<br />";
echo $fourth . "<br />";
echo $fifth . "<br />";
echo $sixth . "<br />";
echo $seventh . "<br />";
echo $eighth . "<br />";
?>
输出:
https:
somesite.com
0
posts
20
20
290
755653-Title.html
https:
somesite.com
0
posts
20
290
755653-Title.html
一种选择是使用 preg_match_all 的正向前瞻,在捕获组中捕获模式:
(?=(/\d+/))
那将匹配
(?=
正向前瞻,断言直接在右边的是
(/\d+/)
匹配 /
, 1+ 位和 /
)
关闭正面前瞻
例如
$re = '~(?=(/\d+/))~m';
$str = 'https://somesite.com/0/posts/20/290/755653-Title.html';
preg_match_all($re, $str, $matches);
print_r($matches[1]);
结果
Array
(
[0] => /0/
[1] => /20/
[2] => /290/
)
如果你只想得到没有周围斜杠的数字,你可以只在数字周围添加组
(?=/(\d+)/)
我们可以尝试在路径分隔符上拆分,然后使用 array_filter
和内联函数来仅保留纯数字部分:
$str = 'https://somesite.com/0/posts/20/290/755653-Title.html';
$parts = explode("/", $str);
$parts = array_filter($parts, function($item) { return is_numeric($item); });
print_r($parts);
这会打印:
Array
(
[3] => 0
[5] => 20
[6] => 290
)
请注意,这种方法完全避免了使用正式的正则表达式,如果您需要在脚本中经常这样做,这可能会对性能产生影响。
假设我有一个 URL:https://somesite.com/0/posts/20/290/755653-Title.html
我如何获得这些变量:/0/, /20/, /290/
?注意它们是变量,它们总是不同的。
我以为我可以这样得到它们:
$url = '//somesite.com/0/posts/20/290/755653-Title.html';
var_dump(parse_url($url));
但数组没有将它们显示为单独的变量。应该用 preg_replace
代替吗?我不认为我知道如何。感谢您的帮助。
您可以使用 explode() 并将字符串转换为由“/”分隔符分隔的数组。
<?php
// Example 1
$url = "https://somesite.com/0/posts/20/290/755653-Title.html";
$pieces = explode("/", $url);
echo $pieces[0] . "<br />";
echo $pieces[1] . "<br />";
echo $pieces[2] . "<br />";
echo $pieces[3] . "<br />";
echo $pieces[4] . "<br />";
echo $pieces[5] . "<br />";
echo $pieces[5] . "<br />";
echo $pieces[6] . "<br />";
echo $pieces[7] . "<br />";
echo "<hr />";
// Example 2
$data = "https://somesite.com/0/posts/20/290/755653-Title.html";
list($first, $second, $third, $fourth, $fifth, $sixth, $seventh, $eighth) = explode("/", $url);
echo $first . "<br />";
echo $second . "<br />";
echo $third . "<br />";
echo $fourth . "<br />";
echo $fifth . "<br />";
echo $sixth . "<br />";
echo $seventh . "<br />";
echo $eighth . "<br />";
?>
输出:
https:
somesite.com
0
posts
20
20
290
755653-Title.html
https:
somesite.com
0
posts
20
290
755653-Title.html
一种选择是使用 preg_match_all 的正向前瞻,在捕获组中捕获模式:
(?=(/\d+/))
那将匹配
(?=
正向前瞻,断言直接在右边的是(/\d+/)
匹配/
, 1+ 位和/
)
关闭正面前瞻
例如
$re = '~(?=(/\d+/))~m';
$str = 'https://somesite.com/0/posts/20/290/755653-Title.html';
preg_match_all($re, $str, $matches);
print_r($matches[1]);
结果
Array
(
[0] => /0/
[1] => /20/
[2] => /290/
)
如果你只想得到没有周围斜杠的数字,你可以只在数字周围添加组
(?=/(\d+)/)
我们可以尝试在路径分隔符上拆分,然后使用 array_filter
和内联函数来仅保留纯数字部分:
$str = 'https://somesite.com/0/posts/20/290/755653-Title.html';
$parts = explode("/", $str);
$parts = array_filter($parts, function($item) { return is_numeric($item); });
print_r($parts);
这会打印:
Array
(
[3] => 0
[5] => 20
[6] => 290
)
请注意,这种方法完全避免了使用正式的正则表达式,如果您需要在脚本中经常这样做,这可能会对性能产生影响。