www.webdeveloper.com
Results 1 to 7 of 7

Thread: parse url

  1. #1
    Join Date
    Feb 2007
    Location
    New Zealand
    Posts
    190

    parse url

    i found this function not long ago, and i've been trying to modify it a little. however, i'm not too good with regular expressions.
    i'm trying to make it also match the filename (if there is one), but everything i've tried has failed. i'm hoping someone here can point me in the right direction.

    PHP Code:
    function parseUrl $url )
    {
        
    $r  '!(?:(\w+)://)?'       //scheme
        
    $r .= '(?:(\w+)\:(\w+)@)?';  //username & password
        
    $r .= '([^/:]+)?';           //domain
        
    $r .= '(?:\:(\d*))?';        //port
        
    $r .= '([^#?]+)?';           //path
        
    $r .= '(?:\?([^#]+))?';      //query
        
    $r .= '(?:#(.+$))?!i';       //fragment
        
    preg_match $r$url$out );
        return 
    $out;


  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Feb 2007
    Location
    New Zealand
    Posts
    190
    i'd prefer to modify this.

    besides basename only returns the last part of the url, be it a filename or just another folder.

  4. #4
    Join Date
    Jun 2007
    Location
    Tangerang, Banten
    Posts
    258
    Is that modified code or the original one?
    You mean want to get the filename?
    I think, you can do it just with echo : $out[2].
    In condition : Path means filename & if it exists.

    If i am not wrong :
    example :
    Code:
    http://test:test123@domain.com:1234/index.php?input=test
    http:// => scheme
    test:test123@ => username & password
    domain.com => domain
    :1234 => port
    /index.php => path
    ?input=test => query
    example 2:
    Code:
    http://test:test123@domain.com:1234/index.php#to-do-more
    #to-do-more => fragment

    Wow... if i am true.. wow.. a nice code...
    Last edited by andre4s_y; 06-01-2008 at 08:30 AM.

  5. #5
    Join Date
    Feb 2007
    Location
    New Zealand
    Posts
    190
    its a snippet off the php site. someone posted it on the parse_url page because it wasn't always working for them. its basically unmodified. i split the regexp up and commented it in my attempt to modify it, but thats all.

    its worked really well for me so far

    just hoping someone can modify it for me


    btw: theres a semi-colon missing in the script above... i can't edit the post anymore

  6. #6
    Join Date
    Jun 2007
    Location
    Tangerang, Banten
    Posts
    258
    Revision to my post before..
    If you want to eliminate subpattern capture, just add "?:" in the subpattern.
    modified code :
    Code:
    function parseUrl ( $url ) 
    { 
        $r  = '!(?:(?:\w+)\://)?';		//scheme 
        $r .= '(?:(?:\w+)\:(?:\w+)@)?';	//username & password 
        $r .= '(?:[^/:]+)?';          	//domain 
        $r .= '(?:\:(?:\d*))?';       	//port 
        //$r .= '([^#?]+)?';      		//path
        $r .= '(?:(?:/[^#?]+)?/([^#?]+))?'; //path
        $r .= '(?:\?(?:[^#]+))?';     	//query 
        $r .= '(?:#(?:.+$))?!i';      	//fragment 
        preg_match ( $r, $url, $out ); 
        return $out; 
    }
    This code will output only full pattern in $out[0] and the filename in $out[1], if exist.

    example :
    Code:
    $url = "http://test:test123@domain.com:1234/test-inc/test2.inc/index.php";
    It will output
    Code:
    Array
    (
        [0] => http://test:test123@domain.com:1234/test-inc/test2.inc/index.php
        [1] => index.php
    )
    The example is in ideal condition. I do not test the "extreme" url yet.. :P
    Last edited by andre4s_y; 06-01-2008 at 12:32 PM.

  7. #7
    Join Date
    Feb 2007
    Location
    New Zealand
    Posts
    190
    ahh its working brilliantly now. thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles