urlを取得してから、urlにparse関数を適用します。 例えば::
$url = 'http://username:[email protected]/path?arg=value#anchor';
print_r(parse_url($url));
Output::
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
このフラグメントは、あなたの条件です。もし私が正しいなら、これを試してみてください。 :) おかげで現在のURLを取得するための
::
function getInstance($uri = 'SERVER')
{
if ($uri == 'SERVER')
{
if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
$https = 's://';
} else {
$https = '://';
}
if (!empty ($_SERVER['PHP_SELF']) && !empty ($_SERVER['REQUEST_URI'])) {
$theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if (strlen($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING']) === false) {
$theURI .= '?'.$_SERVER['QUERY_STRING'];
}
}
else
{
$theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
$theURI .= '?' . $_SERVER['QUERY_STRING'];
}
}
$theURI = urldecode($theURI);
$theURI = str_replace('"', '"',$theURI);
$theURI = str_replace('<', '<',$theURI);
$theURI = str_replace('>', '>',$theURI);
$theURI = preg_replace('/eval\((.*)\)/', '', $theURI);
$theURI = preg_replace('/[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']/', '""', $theURI);
}
echo (string)$theURI;
}
正解は、そのURLをPHPでどのように利用できるようにするかによってまったく異なります。どうか明らかにしてください。 [これは少なくとも四つ組です](http://stackoverflow.com/search?q=fragment+url+php)ので、あなたが試したアプローチのうちのどれを試してみたのか、なぜそれがまだ機能していないのかを指摘してください。 – Gordon
[PHP&Hash/Fragment URL of URL]の複製が可能です。(URL:http://stackoverflow.com/questions/1162008/php-hash-fragment-portion-of-url) – Gordon
どのURLを解析しようとしていますか?現在のページのURL、または別の場所から取得したURLですか? – Gareth