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.
If it's only about parsing a known URL from whatever source, the answer by mck89 is perfectly fine though.
B) 您想通过向 PHP 发送表单来获取 #hash 吗? =>使用一些 JavaScript 魔法! (预处理表单)
var forms = document.getElementsByTagName('form'); //get all forms on the site
for (var i = 0; i < forms.length; i++) { //to each form...
forms[i].addEventListener( // add a "listener"
'submit', // for an on-submit "event"
function () { //add a submit pre-processing function:
var input_name = "fragment"; // name form will use to send the fragment
// Try search whether we already done this or not
// in current form, find every <input ... name="fragment" ...>
var hiddens = form.querySelectorAll('[name="' + input_name + '"]');
if (hiddens.length < 1) { // if not there yet
//create an extra input element
var hidden = document.createElement("input");
//set it to hidden so it doesn't break view
hidden.setAttribute('type', 'hidden');
//set a name to get by it in PHP
hidden.setAttribute('name', input_name);
this.appendChild(hidden); //append it to the current form
} else {
var hidden = hiddens[0]; // use an existing one if already there
}
//set a value of #HASH - EVERY TIME, so we get the MOST RECENT #hash :)
hidden.setAttribute('value', window.location.hash);
}
);
}
B) You want to get a #hash by sending a form to PHP? => Use some JavaScript MAGIC! (To pre-process the form)
var forms = document.getElementsByTagName('form'); //get all forms on the site
for (var i = 0; i < forms.length; i++) { //to each form...
forms[i].addEventListener( // add a "listener"
'submit', // for an on-submit "event"
function () { //add a submit pre-processing function:
var input_name = "fragment"; // name form will use to send the fragment
// Try search whether we already done this or not
// in current form, find every <input ... name="fragment" ...>
var hiddens = form.querySelectorAll('[name="' + input_name + '"]');
if (hiddens.length < 1) { // if not there yet
//create an extra input element
var hidden = document.createElement("input");
//set it to hidden so it doesn't break view
hidden.setAttribute('type', 'hidden');
//set a name to get by it in PHP
hidden.setAttribute('name', input_name);
this.appendChild(hidden); //append it to the current form
} else {
var hidden = hiddens[0]; // use an existing one if already there
}
//set a value of #HASH - EVERY TIME, so we get the MOST RECENT #hash :)
hidden.setAttribute('value', window.location.hash);
}
);
}
Depending on your form's method attribute you get this hash in PHP by:
$_GET['fragment'] or $_POST['fragment']
Possible returns: 1. ""[empty string] (no hash) 2. whole hash INCLUDING the #[hash] sign (because we've used the window.location.hash in JavaScript which just works that way :) )
C) You want to get the #hash in PHP JUST from requested URL?
YOU CAN'T !
...(not while considering regular HTTP requests)...
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.html the following...
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.
It may have other caveats and what not ... but I think that at least doing something with the # on the server is possible.
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
<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>
:然后,在表单提交时,您可以通过 $_POST['hash'] 检索值(设置表单)
You can do it by a combination of javascript and 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)
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.
setup your strings with a div id, so the name anchor goes where its supposed to and the JavaScript can change the text colors
<div id="tesla">Tesla</div>
<div id="tesla1">An energy company</div>
Use JavaScript to do the heavy work, on the server side, inserted in your PHP page, or wherever..
This works. client clicks link on client side (example.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..
发布评论
评论(10)
如果您想获取用户浏览器中显示的哈希标记或锚点之后的值:这对于“标准”HTTP 来说是不可能的,因为该值永远不会发送到服务器(因此它在
中不可用>$_SERVER["REQUEST_URI"]
或类似的预定义变量)。您需要在客户端使用某种 JavaScript 魔法,例如将此值作为 POST 参数包含在内。如果只是从任何来源解析已知的 URL,则 答案mck89 制作的 完全没问题。
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.If it's only about parsing a known URL from whatever source, the answer by mck89 is perfectly fine though.
该部分称为“片段”,您可以通过以下方式获取它:
That part is called "fragment" and you can get it in this way:
A) PHP 中已经有带 #hash 的 url 了吗?简单的!简单解析一下吧!
或者在“旧”PHP 中,您必须预先存储分解后才能访问数组:
B) 您想通过向 PHP 发送表单来获取 #hash 吗?
=>使用一些 JavaScript 魔法! (预处理表单)
根据您的
form
的method
属性,您可以通过以下方式在 PHP 中获取此哈希值:$_GET['fragment']
或$_POST['fragment']
可能的回报: 1.
""< /code>[空字符串](无哈希) 2. 整个哈希,包括
#
[hash] 符号(因为我们在 JavaScript 中使用了window.location.hash
这就是这样工作的:) )C) 您想从请求的 URL JUST 获取 PHP 中的 #hash 吗?
sp; 你不能!
...(不在考虑常规 HTTP 请求时)...
...希望这有帮助:)
A) already have url with #hash in PHP? Easy! Just parse it out !
Or in "old" PHP you must pre-store the exploded to access the array:
B) You want to get a #hash by sending a form to PHP?
=> Use some JavaScript MAGIC! (To pre-process the form)
Depending on your
form
'smethod
attribute you get this hash in PHP by:$_GET['fragment']
or$_POST['fragment']
Possible returns: 1.
""
[empty string] (no hash) 2. whole hash INCLUDING the#
[hash] sign (because we've used thewindow.location.hash
in JavaScript which just works that way :) )C) You want to get the #hash in PHP JUST from requested URL?
YOU CAN'T !
...(not while considering regular HTTP requests)...
...Hope this helped :)
我一直在寻找解决此问题的方法 - 我发现的唯一方法是使用 URL 重写来读取“锚点”。我在apache文档中找到了http://httpd.apache.org/docs/2.2/rewrite/advanced .html 以下...
它可能还有其他警告,但我认为至少可以在服务器上使用 # 做一些事情。
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.html the following...
It may have other caveats and what not ... but I think that at least doing something with the # on the server is possible.
您无法获取井号标记之后的文本。它不会在请求中发送到服务器。
You can't get the text after the hash mark. It is not sent to the server in a request.
如果你坚持想要 PHP 的值,我发现了这个技巧。
分割锚点(
#
)值并用JavaScript获取它,然后存储为cookie,然后用PHP获取cookie值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如果您想从 URL 动态获取哈希值,这应该可行:
https://stackoverflow.com/a/57368072/2062851< /a>
If you are wanting to dynamically grab the hash from URL, this should work:
https://stackoverflow.com/a/57368072/2062851
你可以通过 javascript 和 php 的组合来做到这
一点
:然后,在表单提交时,您可以通过 $_POST['hash'] 检索值(设置表单)
You can do it by a combination of javascript and php:
And by the other side;
Then, on form submit you can retrieve the value via $_POST['hash'] (set the form)
需要先解析url,所以是这样的:
如果需要解析当前浏览器的实际url,就需要请求调用服务器。
You need to parse the url first, so it goes like this:
If you need to parse the actual url of the current browser, you need to request to call the server.
获取查询字符串中哈希标记后面的数据很简单。以下是客户访问书中的术语表时使用的示例。它采用提供的名称锚点 (#tesla),并向客户提供该术语,并以蓝色突出显示该术语及其描述,以便于查看。
使用 div id 设置字符串,以便名称锚点位于其应有的位置,并且 JavaScript 可以更改文本颜色
使用 JavaScript 来完成繁重的工作,可以在服务器端、插入 PHP 页面或任何地方。 .
加载页面时,我会自动启动 Java 函数。
<前><代码><脚本>;
$( 文档 ).ready(function() {
从服务器收到的 URL 中获取锚点 (#tesla)
修剪其哈希符号
我需要突出显示该术语和描述,以便创建一个新的变量
现在我可以操纵术语和描述的文本颜色
这有效。客户点击客户端的链接 (
example.com#tesla
) 并直接转到该术语。该术语和描述由 JavaScript 以蓝色突出显示,以便快速阅读。所有其他条目均保留为黑色。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.
setup your strings with a div id, so the name anchor goes where its supposed to and the JavaScript can change the text colors
Use JavaScript to do the heavy work, on the server side, inserted in your PHP page, or wherever..
I am launching the Java function automatically when the page is loaded.
get the anchor (#tesla) from the URL received by the server
trim the hash sign off of it
I need to highlight the term and the description so I create a new var
Now I can manipulate the text color for the term and description
This works. client clicks link on client side (
example.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..