2012-07-31 57 views
1

可能重複:
Why the hash part of the URL is not in the server side?如何使用散列提取URL參數?

我有一個URL http://www.example.com/edit-your-profile/#file%5B0%5D%5Bstatus%5D=Complete&file%5B0%5D%5BremoteFileURL%5D=http%3A%2F%2Fi.imgur.com%2Fe0XJI.jpg&file%5B0%5D%5BfileSource%5D=Previous%20Uploads&file%5B0%5D%5BpicID%5D=p43

我需要呼應或在頁面上顯示的 「remoteFileURL」 參數的輸入值字段,看來我不能用PHP做(據我所知)。我不知道js很好,任何幫助將不勝感激!

+2

您是在談論您所在頁面的實際URL /地址,還是僅僅是您從somwhere解析過的任意鏈接,並希望將其視爲一個字符串來提取該信息? – simbabque 2012-07-31 08:30:49

+0

+1,你應該訪問@ deceze的鏈接! – Florent 2012-07-31 08:36:41

回答

0

您可以使用正則表達式來搜索與「remoteFileUrl」匹配的任何內容,然後再匹配某些字符,然後將字符串拆分爲每個符號...,然後僅將該文本字符串回送到輸入字段中。

0

如果您在Javascript想要它,這裏有一個簡單的正則表達式的位,將你弄什麼:

unescape(decodeURI(window.location.hash).match(/remoteFileURL]=([^&]+)/)[1]) 

decodeURI(window.location.hash).match(/remoteFileURL]=([^&]+)/)[1] 

這對我來說

所以我們使用unescape返回"http%3A%2F%2Fi.imgur.com%2Fe0XJI.jpg"

得到"http://i.imgur.com/e0XJI.jpg"

3

簡化 - 不是美麗......只是爲了展示一種可能性並指引您朝着正確的方向前進。返回http://i.imgur.com/e0XJI.jpg

<?php 
$url = 'http://www.example.com/edit-your-profile/#file%5B0%5D%5Bstatus%5D=Complete&file%5B0%5D%5BremoteFileURL%5D=http%3A%2F%2Fi.imgur.com%2Fe0XJI.jpg&file%5B0%5D%5BfileSource%5D=Previous%20Uploads&file%5B0%5D%5BpicID%5D=p43'; 
$urlDecoded = urldecode($url); 
$urlParts = parse_url($urlDecoded); 

$matches = array(); 
$regexRemoteUrl = preg_match('/remoteFileUrl\]=([^&]+)/i', $urlParts['fragment'], $matches); 
// remoteFileURL 
echo($matches[1]); 
?> 
+0

這個作品真棒!有關我如何動態獲取URL的任何想法? PHP將停止在哈希標記,我不知道如何將document.URL包含到此代碼中 – Casey 2012-07-31 08:57:30

+0

@Casey [爲什麼URL的哈希部分不在服務器端?](http://stackoverflow.com/問題/ 3664257 /爲什麼最哈希部分的最網址 - 是 - 不中,在服務器端) – deceze 2012-07-31 09:32:47

0
var decodedUri = decodeURIComponent('http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg'); 

您可以使用此功能來提取ü從URL所需要的參數。

function getURLParameter(name,url) { 
    return decodeURI(
     (RegExp(name + '=' + '(.+?)(&|$)').exec(decodedUri)||[,null])[1] 
    ); 
} 

調用函數如下

var fileUrl = getURLParameter("remoteFileURL",decodedUri); 

:)

0

這裏有一個正則表達式,你可以在JavaScript中使用或php:

(?:file%5B0%5D%5BremoteFileURL%5D|remoteFileURL)=(.+?)& 

因爲你的榜樣URL有非常奇怪的參數命名形式,我包括這種形式和通常的方式。它匹配file[0][remoteFile]以及remoteFile並捕獲該參數的值。

在JavaScript中,您可以這樣做,如果有一個字段包含ID爲URL的網址。在Rubular

var url = document.getElementById('URL').value; 
var myRegexp = /(?:file%5B0%5D%5BremoteFileURL%5D|remoteFileURL)=(.+?)\&/; 
var match = myRegexp.exec(url); 
alert(match[1]);