如何从php中的URL获取片段(散列'#'后的值)?
从http://domain.com/site/gallery/1#photo45
说我想要photo45
如果你想在用户的浏览器中显示哈希标记或锚之后的值:使用“标准”HTTP是不可能的,因为这个值永远不会发送到服务器(因此它不会在$_SERVER["REQUEST_URI"]
或类似的预定义变量)。您需要在客户端使用某种JavaScript魔法,例如:将此值包含为POST参数。
如果它只是从任何来源解析一个已知的URL,那么answer by mck89完全没问题。
你可以通过javascript和php的组合来做到这一点:
<div id="cont"></div>
另一方面;
<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']检索值(设置表单)
在查询字符串中获取hashmark之后的数据很简单。以下是客户端从书籍访问术语表时使用的示例。它采用名称锚传递(#tesla),并将客户端传递给该术语,并以蓝色突出显示术语及其描述,因此很容易看到。
A.使用div id设置你的字符串,所以名称锚定位于它应该的位置,javascript可以改变文本颜色
<div id="tesla">Tesla</div>
<div id="tesla1">An energy company</div>
B.使用Javascript完成繁重的工作,在服务器端,插入PHP页面或任何地方..
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
C.我在加载页面时自动启动java函数。
<script>
$( document ).ready(function() {
D.从服务器收到的url中获取锚点(#tesla)
var myhash1 = $(location).attr('hash'); //myhash1 == #tesla
E.修剪哈希标志
myhash1 = myhash1.substr(1) //myhash1 == tesla
F.我需要突出显示术语和描述,因此我创建了一个新的var
var myhash2 = '1';
myhash2 = myhash1.concat(myhash2); //myhash2 == tesla1
G.现在我可以操纵术语和描述的文本颜色
var elem = document.getElementById(myhash1);
elem.style.color = 'blue';
elem = document.getElementById(myhash2);
elem.style.color = 'blue';
});
</script>
H.这很有效。客户端点击客户端链接(xyz.com#tesla)并直接进入该术语。该术语和描述以蓝色突出显示,以便快速阅读..所有其他条目留在黑色..
那部分被称为“片段”,你可以这样得到它:
$url=parse_url("http://domain.com/site/gallery/1#photo45 ");
echo $url["fragment"]; //This variable contains the fragment
if( strpos( $url, "#" ) === false ) echo "NO HASH !";
else echo "HASH IS: #".explode( "#", $url )[1]; // arrays are indexed from 0
或者在“旧”PHP中,您必须预先存储爆炸以访问数组:
$exploded_url = explode( "#", $url ); $exploded_url[1];
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
});
根据您的form
的method
属性,您可以通过以下方式在PHP中获取此哈希:
$_GET['fragment']
或$_POST['fragment']
可能的返回:1。""
[空字符串](无哈希)2。整个哈希包含#
[哈希]符号(因为我们在JavaScript中使用了window.location.hash
,只是这样工作:))
...(不是在考虑常规HTTP请求时)......
...希望这有帮助:)
我一直在寻找一个解决方法 - 我发现的唯一的事情就是使用URL重写来读取“锚点”。我发现在这里的apache docs http://httpd.apache.org/docs/2.2/rewrite/advanced.html以下......
默认情况下,重定向到HTML锚点不起作用,因为mod_rewrite转义#字符,将其转换为%23。反过来,这会打破重定向。
解决方案:使用RewriteRule上的[NE]标志。 NE代表No Escape。
讨论:这种技术当然也适用于mod_rewrite默认情况下URL编码的其他特殊字符。
它可能有其他警告,但不是......但我认为至少可以使用服务器上的#做一些事情。
您无法在哈希标记后获取文本。它不会在请求中发送到服务器。
我找到了这个技巧,如果你坚持要用php的值...分割锚(#)值并用javascript获取它,然后存储为cookie,之后用php获取cookie值〜
http://www.stoimen.com/blog/2009/04/15/read-the-anchor-part-of-the-url-with-php/
你需要先解析网址,所以它是这样的:
$url = "https://www.example.com/profile#picture";
$fragment = parse_url($url,PHP_URL_FRAGMENT); //this variable holds the value - 'picture'
如果需要解析当前浏览器的实际URL,则需要请求调用服务器。
$url = $_SERVER["REQUEST_URI"];
$fragment = parse_url($url,PHP_URL_FRAGMENT); //this variable holds the value - 'picture'
如果你想从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;
?>
您可以在客户端然后在服务器端执行字符串替换。不是一个特别强大的解决方案,但如果你不像我这样的快速解决方案,我认为就足够了。
客户:
var tempString = stringVal.replace('#', 'hashtag');
服务器:
$user_message = $_GET['userMessage'];
$user_message = str_replace("hashtag", "#", $user_message);