Skip to main content

How to extract relative URL from argument values from request string?



I have a request url / string var like http://ip_or_url:4773/image_renderer.service?action=resize&from_format=png&from_url=http://ip_or_url:4773/my_file.user.file&to_format=jpg&w=1920&h=1200 It looks terribly scary. I wonder how to extract from_url=http://195.19.243.13:4773/my_file.user.file argument pair from it and then extract relative file url from that pair value my_file.user.file ?




Comments

  1. With pure javascript. It'll return an array of key/value pairs.

    function getUrlParts(url){
    // url contains your data.
    var qs = url.indexOf("?");
    if(qs==-1) return [];
    var fr = url.indexOf("#");
    var q="";
    q = (fr==-1)? url.substr(qs+1) : url.substr(qs+1, fr-qs-1);
    var parts=q.split("&");
    var vars={};
    for(var i=0;i<parts.length; i++){
    var p = parts[i].split("=");
    vars[unescape(p[0])]= p[1] ? unescape(p[1]):"";
    }
    // vars contain all the variables in an array.
    return vars;
    }


    Let me know if any of your test cases fails.

    ReplyDelete
  2. When the url is in the varaible sUrl:

    var sFromUrl = sUrl.match(/from_url=[^&]*/)[0];
    var sBasename = sFromUrl.match(/[^\/]*$/)[0];


    Also see this example.

    ReplyDelete
  3. Use this to get your url variables in JS

    function getUrlVars()
    {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
    }

    return vars;
    }


    and then string.substring(string.search("/")) to get your piece of string

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.