php - Converting relative URL to absolute -


Assume that I have a URL of a document that connects to another document (which can be both full or relative ) And I need it

($ parent_url); If (strcmp (substr ($ url, 0,7), 'http: //') == 0) {return $ url; } Otherwise (strcmp (substr ($ url, 0,1), '/') == 0) $ parent_url ['plan']. "$ .parent_url ['host']. $ Url;} and {$ path = $ parent_url ['path']; $ path = substr ($ path, 0, strrpos ($ path, '/')) ; Return $ parent_url ['Plan']. ": //". $ Parent_url ['Host']. "$ Path /". $ Url;}} $ Parent_url = 'http: //example.com/path/to /file/name.php? Abc = abc '; echo absolute_url (' name2.php ', $ parent_url). "\ N"; // output http://example.com/path/to/file/name2.php Echo absolute_url ('/ name2.php', $ parent_url). "\ N"; // output http://example.com/name2.php echo absolute_url ('http: //name2.php',$parent_url). "\ N"; // output http: //name2.php

this code works fine, but ../../ path / to / file.php There may be more cases like this which will not work.

Is there any such thing Is there a class or is this work better (more universal) than that which is my work?

I tried it Google and tested the same question (and) but it's like a server-path solution I do not see what I see.

$ uri = ".."; $ Path = real path ($ uri); $ Root = realpath ($ _ server ["DOCUMENT_ROOT"]); If ($ path) {$ path = str_replace ($ root, "", $ path); $ Path = $ _SERVER ["SERVER_NAME"] $ path; $ Protocol = "http"; If (isset ($ _ server ["HTTPS"])) {$ protocol} = "S"; } $ Path = "{$ protocol}: // $ path"; $ Path = str_replace ("\\", "/", $ path); } Var_dump ($ path);

Maybe there's a better / faster way, but I dismiss it ...


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -