从 php 中的 URL 获取片段(散列“#”后的值)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2317508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get fragment (value after hash '#') from a URL in php
提问by ilija veselica
How can I get the fragment (value after hash '#') from a URL in php?
如何从 php 中的 URL 获取片段(散列“#”后的值)?
Say from http://domain.com/site/gallery/1#photo45I want photo45
说从http://domain.com/site/gallery/1#photo45我想要photo45
回答by sfussenegger
If you want to get the value after the hash mark or anchor as shown in a user's browser: This isn't possible with "standard" HTTP as this value is never sent to the server (hence it won't be available in $_SERVER["REQUEST_URI"]or similar predefined variables). You would need some sort of JavaScript magic on the client side, e.g. to include this value as a POST parameter.
如果您想获取用户浏览器中显示的哈希标记或锚点后的值:“标准”HTTP 无法做到这一点,因为此值永远不会发送到服务器(因此它不会在$_SERVER["REQUEST_URI"]或类似的情况下可用)预定义变量)。您需要在客户端使用某种 JavaScript 魔法,例如将此值作为 POST 参数包含在内。
If it's only about parsing a known URL from whatever source, the answer by mck89is perfectly fine though.
回答by mck89
That part is called "fragment" and you can get it in this way:
该部分称为“片段”,您可以通过以下方式获得它:
$url=parse_url("http://domain.com/site/gallery/1#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
回答by jave.web
A) already have url with #hash in PHP? Easy! Just parse it out !
A) 在 PHP 中已经有带有 #hash 的 url?简单!只要解析出来!
if( strpos( $url, "#" ) === false ) echo "NO HASH !";
else echo "HASH IS: #".explode( "#", $url )[1]; // arrays are indexed from 0
Or in "old" PHP you must pre-store the exploded to access the array:
或者在“旧” PHP 中,您必须预先存储爆炸来访问数组:
$exploded_url = explode( "#", $url ); $exploded_url[1];
B) You want to get a #hash by sending a form to PHP?
=> Use some JavaScript MAGIC! (To pre-process the form)
B) 您想通过向 PHP 发送表单来获得 #hash 吗?
=> 使用一些 JavaScript 魔法!(对表格进行预处理)
var forms = document.getElementsByTagName('form'); //get all forms on the site
for(var i=0; i<forms.length;i++) forms[i].addEventListener('submit', //to each form...
function(){ //add a submit pre-processing function that will:
var hidden = document.createElement("input"); //create an extra input element
hidden.setAttribute('type','hidden'); //set it to hidden so it doesn't break view
hidden.setAttribute('name','fragment'); //set a name to get by it in PHP
hidden.setAttribute('value',window.location.hash); //set a value of #HASH
this.appendChild(hidden); //append it to the current form
});
Depending on your form's methodattribute you get this hash in PHP by:
$_GET['fragment']or $_POST['fragment']
根据您的form'smethod属性,您可以通过以下方式在 PHP 中获得此哈希值:
$_GET['fragment']或$_POST['fragment']
Possible returns:1. ""[empty string] (no hash) 2. whole hash INCLUDING the #[hash] sign (because we've used the window.location.hashin JavaScript which just works that way :) )
可能的返回:1. ""[空字符串](无散列) 2. 整个散列,包括#[hash] 符号(因为我们window.location.hash在 JavaScript 中使用了这种方式 :) )
C) You want to get the #hash in PHP JUSTfrom requested URL?
C)你想在PHP中#hash JUST从请求的URL?
YOU CAN'T !
你不能!
...(not while considering regular HTTP requests)...
...(不是在考虑常规 HTTP 请求时)...
...Hope this helped :)
...希望这有帮助:)
回答by Bronson
I've been searching for a workaround for this for a bit - and the only thing I have found is to use URL rewrites to read the "anchor". I found in the apache docs here http://httpd.apache.org/docs/2.2/rewrite/advanced.htmlthe following...
我一直在寻找一种解决方法 - 我发现的唯一方法是使用 URL 重写来读取“锚点”。我在http://httpd.apache.org/docs/2.2/rewrite/advanced.html的apache 文档中找到了以下内容...
By default, redirecting to an HTML anchor doesn't work, because mod_rewrite escapes the # character, turning it into %23. This, in turn, breaks the redirection.
Solution: Use the [NE] flag on the RewriteRule. NE stands for No Escape.
Discussion: This technique will of course also work with other special characters that mod_rewrite, by default, URL-encodes.
默认情况下,重定向到 HTML 锚点不起作用,因为 mod_rewrite 会转义 # 字符,将其转换为 %23。这反过来会破坏重定向。
解决方案:使用 RewriteRule 上的 [NE] 标志。NE 代表无逃逸。
讨论:这种技术当然也适用于 mod_rewrite 的其他特殊字符,默认情况下,URL 编码。
It may have other caveats and what not ... but I think that at least doing something with the # on the server is possible.
它可能有其他警告,但我认为至少可以在服务器上用 # 做一些事情。
回答by Ignacio Vazquez-Abrams
You can't get the text after the hash mark. It is not sent to the server in a request.
您无法获取井号后的文本。它不会在请求中发送到服务器。
回答by jihchuan
I found this trick if you insist want the value with PHP.
split the anchor (#) value and get it with JavaScript, then store as cookie, after that get the cookie value with PHP
如果你坚持想要 PHP 的价值,我发现了这个技巧。拆分锚( #) 值并用JavaScript 获取,然后存储为cookie,然后用PHP 获取cookie 值
回答by samjco
If you are wanting to dynamically grab the hash from URL, this should work: https://stackoverflow.com/a/57368072/2062851
如果您想从 URL 动态获取哈希,这应该可以工作:https: //stackoverflow.com/a/57368072/2062851
<script>
var hash = window.location.hash, //get the hash from url
cleanhash = hash.replace("#", ""); //remove the #
//alert(cleanhash);
</script>
<?php
$hash = "<script>document.writeln(cleanhash);</script>";
echo $hash;
?>
回答by JJ Labajo
You need to parse the url first, so it goes like this:
你需要先解析url,所以它是这样的:
$url = "https://www.example.com/profile#picture";
$fragment = parse_url($url,PHP_URL_FRAGMENT); //this variable holds the value - 'picture'
If you need to parse the actual url of the current browser, you need to request to call the server.
如果需要解析当前浏览器的实际url,则需要请求调用服务器。
$url = $_SERVER["REQUEST_URI"];
$fragment = parse_url($url,PHP_URL_FRAGMENT); //this variable holds the value - 'picture'
回答by Boris Javier Barrera
You can do it by a combination of javascript and php:
您可以通过 javascript 和 php 的组合来完成:
<div id="cont"></div>
And by the other side;
而在另一边;
<script>
var h = window.location.hash;
var h1 = (win.substr(1));//string with no #
var q1 = '<input type="text" id="hash" name="hash" value="'+h1+'">';
setInterval(function(){
if(win1!="")
{
document.querySelector('#cont').innerHTML = q1;
} else alert("Something went wrong")
},1000);
</script>
Then, on form submit you can retrieve the value via $_POST['hash'] (set the form)
然后,在表单提交时,您可以通过 $_POST['hash'] 检索值(设置表单)
回答by JamesAD-0
Getting the data after the hashmark in a query string is simple. Here is an example used for when a client accesses a glossary of terms from a book. It takes the name anchor delivered (#tesla), and delivers the client to that term and highlights the term and its description in blue so its easy to see.
在查询字符串中的哈希标记之后获取数据很简单。这是一个示例,用于当客户从书中访问术语表时。它采用已交付的名称锚点 (#tesla),并将客户交付给该术语,并以蓝色突出显示该术语及其描述,以便于查看。
A. setup your strings with a div id, so the name anchor goes where its supposed to and the javascript can change the text colors
A. 用一个 div id 设置你的字符串,这样名字锚就在它应该去的地方,javascript 可以改变文本颜色
<div id="tesla">Tesla</div>
<div id="tesla1">An energy company</div>
B. Use Javascript to do the heavy work, on the server side, inserted in your PHP page, or wherever..
B. 使用 Javascript 来做繁重的工作,在服务器端,插入到你的 PHP 页面中,或者任何地方..
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
C. I am launching the java function automatically when the page is loaded.
C、我在页面加载时自动启动java函数。
<script>
$( document ).ready(function() {
D. get the anchor (#tesla) from the url received by the server
D.从服务器收到的url中获取锚点(#tesla)
var myhash1 = $(location).attr('hash'); //myhash1 == #tesla
E. trim the hash sign off of it
E. 修剪掉它的哈希符号
myhash1 = myhash1.substr(1) //myhash1 == tesla
F. I need to highlight the term and the description so i create a new var
F. 我需要突出显示术语和描述,所以我创建了一个新的变量
var myhash2 = '1';
myhash2 = myhash1.concat(myhash2); //myhash2 == tesla1
G. Now I can manipulate the text color for the term and description
G. 现在我可以操纵术语和描述的文本颜色
var elem = document.getElementById(myhash1);
elem.style.color = 'blue';
elem = document.getElementById(myhash2);
elem.style.color = 'blue';
});
</script>
H. This works. client clicks link on client side (xyz.com#tesla) and goes right to the term. the term and the description are highlighted in blue by javascript for quick reading .. all other entries left in black..
H. 这有效。客户端单击客户端 (xyz.com#tesla) 上的链接并直接转到该术语。javascript 用蓝色突出显示该术语和描述,以便快速阅读.. 所有其他条目都保留为黑色..

