Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I wrote this mainly because someone asked how to do this in PHP. Turns out there is nothing in like the venerable URI module in all of PHP-dom (how pathetic is that for a language as web-centric as it?).

First, I tried writing this with mostly string functions, but that was too painful, so I switched to doing it using regexes, even though they’re piss-poorly supported. After a while, my eyes glazed over, so I resorted to writing and debugging the code in Perl first, and then compiling it down to machine code PHP.

I thought I’d post this byproduct here for posterity, in case someone needs it in some circumstance.

Tests included, but I’m pretty sure they’re not extensive enough to be 100% confident in the code. Proceed at your discretion.

sub abs_url { my ( $relative, $base ) = @_; return $relative if $relative =~ m{ \A http:// }ix; my ( $host, $hostrelative_abs ) = $base =~ m{ \A http:// # skip scheme ([^/]*) # capture hostname /* # skip front slashes (.*?) # capture everything that follows, but [^/]* # leave out the optional final non-directory component \z }ix; $hostrelative_abs = '' if $relative =~ m!^/!; my $abs_url = join '/', $host, $hostrelative_abs, $relative; # replace '//' or '/./' with '/' 1 while $abs_url =~ s{ / \.? (?=/|\z) }{}x; # remove '/foo/..' (but be careful to skip '/../..') 1 while $abs_url =~ s{ / (?!\.\.) [^/]+ / \.\. (?=/|\z) }{}x; return "http://$abs_url"; }
use Test::More; my @test = qw( /foo/bar.gif http://www.example.com/baz/quux/ http://www.exam +ple.com/foo/bar.gif foo/bar.gif http://www.example.com/baz/quux/ http://www.exam +ple.com/baz/quux/foo/bar.gif ../../bar.gif http://www.example.com/baz/quux/ http://www.exam +ple.com/bar.gif foo/bar.gif http://www.example.com/baz/quux http://www.exam +ple.com/baz/foo/bar.gif ../foo/bar.gif http://www.example.com/baz/quux http://www.exam +ple.com/foo/bar.gif ../../foo/bar.gif http://www.example.com/baz/quux/qux http://www.exam +ple.com/foo/bar.gif ); plan tests => @test / 3; do { is abs_url( $test[0], $test[1] ), $test[2]; splice @test, 0, 3 } +while @test;

In reply to Convert relative to absolute URL given a base URL, without modules by Aristotle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found