如何在字符串中提取一些数据?使用PHP

时间:2017-07-04 10:05:26

标签: php string

我有一串URL格式。

就是这个。 “http://test.com/foo/bar/detail.php?key=KEY&other=x

我需要从URL中提取“KEY”。

与此类似。

$foo = $_GET['key'];

但我所拥有的是一串URL格式 我如何提取KEY这个字符串?

1 个答案:

答案 0 :(得分:0)

您可以使用parse_url()http://php.net/manual/en/function.parse-url.phpparse-str http://php.net/manual/en/function.parse-str.php

$string = "http://test.com/foo/bar/detail.php?key=KEY&other=x";
$query = parse_url($url, PHP_URL_QUERY);
$variable = parse_str($query, $output);
echo $output['key'];