1

Possible Duplicate:
Why the hash part of the URL is not in the server side?

I have a 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

I need to echo or display the "remoteFileURL" parameter on the page as the value of a input field, It seems I cant do it with PHP(as far as I know). I dont know js very well, any help would be appreciated!

Community
  • 1
  • 1
Casey
  • 35
  • 5
  • 2
    Are you talking about the actual URL/address of the page you are on, or is this just an arbitrary link that you have parsed from somwhere and want to treat as a string to extract that info? – simbabque Jul 31 '12 at 08:30
  • +1, You should definitely visit @deceze's link! – Florent Jul 31 '12 at 08:36

5 Answers5

3

simplified - not beauty... just for showing a possibility and pointing you in the right direction. Returns 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]);
?>
thedom
  • 2,498
  • 20
  • 26
  • This works awesome! Any idea on how I can dynamically get the URL? PHP will stop at the hash tag, and I am not sure how to include document.URL into this code – Casey Jul 31 '12 at 08:57
  • @Casey [Why the hash part of the URL is not in the server side?](http://stackoverflow.com/questions/3664257/why-the-hash-part-of-the-url-is-not-in-the-server-side) – deceze Jul 31 '12 at 09:32
0

You can use regex to search for anything that matches "remoteFileUrl" followed by some characters...and then split the string at each ampersand...and then simply echo that text string into your input field.

techtheatre
  • 5,678
  • 7
  • 31
  • 51
0

If you wanted it in Javascript, here's a simply bit of regex that will get it for you:

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

It returns "http%3A%2F%2Fi.imgur.com%2Fe0XJI.jpg" for me

so we use unescape:

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

to get "http://i.imgur.com/e0XJI.jpg"

paulslater19
  • 5,869
  • 1
  • 28
  • 25
0
var decodedUri = decodeURIComponent('http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg');

You can use this function to extract the parameter u need from a url.

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

Invoke the function as follows

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

:)

Rahmathullah M
  • 2,676
  • 2
  • 27
  • 44
0

Here's a regular expression you can use in JavaScript or php:

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

Since your example URL has a very strange form of naming the parameters, I included both that form and the usual way. It matches for file[0][remoteFile] as well as remoteFile and captures the value of that parameter.

In JavaScript you can do it like this, if there's a field containing the URL with the id URL.

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