用php检索网址中的哈希值? [英] retrieve the hash in the url with php?

查看:557
本文介绍了用php检索网址中的哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有人告诉我,url中的哈希值(#)可以通过php函数parse_url检索到吗?这是真的吗?



我的网站获得了很多ajax效果,我希望能够将带有哈希的url传递给index.php,以便它可以显示基于哈希值的信息。



例如。如果用户在我的网站上点击并导航并找到了他想要分享的内容,那么他​​可以复制该地址并将其发送给他的朋友,当这个人进入时,他会看到同样的事情。



我已经尝试使用javascript来保存cookie中的哈希值,但是这并不适用于Firefox和它在Safari中的速度太慢(html会首先显示,所以必须第二次刷新页面正确显示的内容。



欢迎任何其他解决方案



编辑:我很困惑..如果我使用parse_url我必须在php中输入url以获取哈希值?那么有什么意义......我的问题是我的朋友是否可以使用#值输入网址并且php可以处理它?它看起来像是不对,我是对的吗?

解决方案

parse_url()可以从url字符串中获取哈希值。请注意签名:

  mixed parse_url(string $ url [,int $ component = -1])$ ​​b $ b  

您必须已经知道该字符串。例如,来自文档:

 <?php 

$ url ='http:/ ?/用户名:密码@主机名/路径ARG =值#锚';
print_r(parse_url($ url));
echo parse_url($ url,PHP_URL_PATH);

?>

输出

 数组

[方案] => http
[主持人] =>主机名
[用户] =>用户名
[通过] =>密码
[路径] => /路径
[查询] => arg =值
[片段] =>锚点

/路径

请注意fragment键下的条目。


someone here in SO told me that the hash (#) in the url could be retrieved by php function parse_url? is this true?

my web site got a lot of ajax effects and i want to be able to pass an url with a hash to the index.php so it could display the information based on the hash values.

eg. if a user clicks and navigates in my website and he found a thing he wants to share, then he could copy the address and send it to his friend and when this person enters he sees the same thing.

i have tried using javascript to save the hash values in cookies but that isnt working with firefox and its too slow in safari (the html will show first, so one has to refresh the page a second time for the correct content to be shown).

any other solution is welcome

EDIT: im very confused..if i use the parse_url i have to type the url myself in php to get the hash? what is the point then...my question if whether my pal can enter the url with # values and php could process it? it seems like a no, am i right?

解决方案

parse_url() can get the hash from a url string. Note the signature:

mixed parse_url ( string $url [, int $component = -1 ] )

You must already know the string. For instance, from the docs:

<?php

  $url = 'http://username:password@hostname/path?arg=value#anchor';
  print_r(parse_url($url));
  echo parse_url($url, PHP_URL_PATH);

?>

Outputs

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path

Note the entry under key "fragment".

这篇关于用php检索网址中的哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆