2009-08-07 33 views
22

को हल करने के लिए मुझे एक ऐसे फ़ंक्शन की आवश्यकता है जो एक सापेक्ष यूआरएल दिया गया हो और आधार एक पूर्ण यूआरएल देता है। मैंने कई कार्यों को खोजा और पाया है जो इसे अलग-अलग तरीकों से करते हैं।PHP: रिश्तेदार यूआरएल

resolve("../abc.png", "http://example.com/path/thing?foo=bar") 
# returns http://example.com/abc.png 

क्या कोई कैननिक तरीका है?

इस साइट पर मैं पाइथन और सी # के लिए महान उदाहरण देखता हूं, चलिए एक PHP समाधान प्राप्त करते हैं।

+2

यहाँ [एक तुलना] (http://scraperblog.blogspot.com/2012/12/convert- सापेक्ष-urls-to-absolute-in.html) कुछ समाधानों में से एक का उल्लेख किया गया है और इनमें से एक है। – pguardiario

+0

इस कोड ने मेरे लिए चाल बनाई: http://sourceforge.net/projects/absoluteurl/ –

+0

गणराज्य: http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using -php http://stackoverflow.com/questions/11653677/php-relative-urls-to-absolute-urls-conversion-with-eventually-base-href-html-tag http://stackoverflow.com/questions/19618754/परिवर्तित-रिश्तेदार-यूआरएल करने वाली पूर्ण-यूआरएल http://stackoverflow.com/questions/26423904/converting-relative-url-to-absolute – qdinar

उत्तर

8

शायद यह आलेख मदद कर सकता है?

http: // nashruddin.com/PHP_Script_for_Converting_Relative_to_Absolute_URL

संपादित करें: सुविधा के लिए नीचे दिए गए कोड reproduced

<?php 
    function rel2abs($rel, $base) 
    { 
     /* return if already absolute URL */ 
     if (parse_url($rel, PHP_URL_SCHEME) != '' || substr($rel, 0, 2) == '//') return $rel; 

     /* queries and anchors */ 
     if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel; 

     /* parse base URL and convert to local variables: 
     $scheme, $host, $path */ 
     extract(parse_url($base)); 

     /* remove non-directory element from path */ 
     $path = preg_replace('#/[^/]*$#', '', $path); 

     /* destroy path if relative url points to root */ 
     if ($rel[0] == '/') $path = ''; 

     /* dirty absolute URL */ 
     $abs = "$host$path/$rel"; 

     /* replace '//' or '/./' or '/foo/../' with '/' */ 
     $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'); 
     for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} 

     /* absolute URL is ready! */ 
     return $scheme.'://'.$abs; 
    } 
?> 
+0

यह कार्यान्वयन कार्य नहीं करता है यदि आधार URL है: http://foobar.com कोई पीछे/बिना। यह आधार यूआरएल में पोर्ट नंबरों का भी सम्मान नहीं करता है। –

+2

यह किसी भी आरएफसी चश्मा को अनदेखा कर रहा है जो किसी भी तरह से उपयोग में है। मैं कहूंगा कि *** *** अनुमानित यूआरएल या रिश्तेदार है। – hakre

+0

डोमेन की समय सीमा समाप्त हो गई। – Pharap

0
function absoluteUri($Path, $URI) 
{ # Requires PHP4 or better. 
    $URL = parse_url($URI); 
    $Str = "{$URL['scheme']}://"; 

    if (isset($URL['user']) || isset($URL['pass'])) 
     $Str .= "{$URL['user']}:{$URL['pass']}@"; 

    $Str .= $URL['host']; 

    if (isset($URL['port'])) 
     $Str .= ":{$URL['port']}"; 

    $Str .= realpath($URL['path'] . $Path); # This part might have an issue on windows boxes. 

    if (isset($URL['query'])) 
     $Str .= "?{$URL['query']}"; 

    if (isset($URL['fragment'])) 
     $Str .= "#{$URL['fragment']}"; 

    return $Str; 
} 

absoluteUri("../abc.png", "http://example.com/path/thing?foo=bar"); 
# Should return "http://example.com/abc.png?foo=bar" on Linux boxes. 
+2

लिनक्स बॉक्स पर लेकिन विंडोज़ नहीं? यह अब तक का एकमात्र समाधान प्रतीत होता है जो कि विचार करने योग्य भी नहीं है। – pguardiario

4

यदि आपका Have PECL-http, आप http://php.net/manual/en/function.http-build-url.php

उपयोग कर सकते हैं
<?php 
$url_parts = parse_url($relative_url); 
$absolute = http_build_url($source_url, $url_parts, HTTP_URL_JOIN_PATH); 

उदाहरण के लिए:

<?php 
function getAbsoluteURL($source_url, $relative_url) 
{ 
    $url_parts = parse_url($relative_url); 
    return http_build_url($source_url, $url_parts, HTTP_URL_JOIN_PATH); 
} 
echo getAbsoluteURL('http://foo.tw/a/b/c', '../pic.jpg') . "\n"; 
// http://foo.tw/a/pic.jpg 

echo getAbsoluteURL('http://foo.tw/a/b/c/', '../pic.jpg') . "\n"; 
// http://foo.tw/a/b/pic.jpg 

echo getAbsoluteURL('http://foo.tw/a/b/c/', 'http://bar.tw/a.js') . "\n"; 
// http://bar.tw/a.js 

echo getAbsoluteURL('http://foo.tw/a/b/c/', '/robots.txt') . "\n"; 
// http://foo.tw/robots.txt 
+1

FYI करें, 'http_build_url' विधि एक PECL एक्सटेंशन है कि PHP के साथ बंडल नहीं है का हिस्सा है। – mpen

+0

यह वर्णित के रूप में कार्य करता है, लेकिन pecl एक्सटेंशन को स्थापित करते समय देखें। नया रिलीज 2.0 संस्करण अब नेमस्पेस का उपयोग करता है और यह फ़ंक्शन सीधे प्रदान नहीं करता है। इसलिए मैंने एक पुराना संस्करण स्थापित किया और यह मेरे लिए पूरी तरह से काम करता है: 'pecl इंस्टॉल pecl_http-1.7.6' – KTB

1

अन्य उपकरण है कि पहले से ही pguardiario की टिप्पणी में लिंक किए गए पृष्ठ में जुड़े हुए हैं: http://publicmind.in/blog/urltoabsolute/, https://github.com/monkeysuffrage/phpuri

और मैं http://nadeausoftware.com/articles/2008/05/php_tip_how_convert_relative_url_absolute_url में टिप्पणी से अन्य उपकरण पाया है:

require_once 'Net/URL2.php'; 
$base = new Net_URL2('http://example.org/foo.html'); 
$absolute = (string)$base->resolve('relative.html#bar'); 
-1

मैंने देखा upvoted जवाब ऊपर रेगुलर एक्सप्रेशन से है, जो खतरनाक जब यूआरएल के साथ काम किया जा सकता है का उपयोग करता है।

इस समारोह regex बिना $pgurlमें एक दिया वर्तमान पृष्ठ यूआरएल को सापेक्ष URL का समाधान हो जाएगा। यह सफलतापूर्वक विभाजित:

/home.php?example प्रकार,

ही-निर्देशिका nextpage.php प्रकार,

../...../.../parentdir प्रकार,

पूर्ण http://example.net यूआरएल,

और आशुलिपि //example.net यूआरएल

//Current base URL (you can dynamically retrieve from $_SERVER) 
$pgurl = 'http://example.com/scripts/php/absurl.php'; 

function absurl($url) { 
global $pgurl; 
if(strpos($url,'://')) return $url; //already absolute 
if(substr($url,0,2)=='//') return 'http:'.$url; //shorthand scheme 
if($url[0]=='/') return parse_url($pgurl,PHP_URL_SCHEME).'://'.parse_url($pgurl,PHP_URL_HOST).$url; //just add domain 
if(strpos($pgurl,'/',9)===false) $pgurl .= '/'; //add slash to domain if needed 
return substr($pgurl,0,strrpos($pgurl,'/')+1).$url; //for relative links, gets current directory and appends new filename 
} 

function nodots($path) { //Resolve dot dot slashes, no regex! 
$arr1 = explode('/',$path); 
$arr2 = array(); 
foreach($arr1 as $seg) { 
    switch($seg) { 
    case '.': 
    break; 
    case '..': 
    array_pop($arr2); 
    break; 
    case '...': 
    array_pop($arr2); array_pop($arr2); 
    break; 
    case '....': 
    array_pop($arr2); array_pop($arr2); array_pop($arr2); 
    break; 
    case '.....': 
    array_pop($arr2); array_pop($arr2); array_pop($arr2); array_pop($arr2); 
    break; 
    default: 
    $arr2[] = $seg; 
    } 
} 
return implode('/',$arr2); 
} 

प्रयोग उदाहरण:

echo nodots(absurl('../index.html')); 

nodots() के बाद URL संपूर्ण में बदल जाती है बुलाया जाना चाहिए।

डॉट्स फ़ंक्शन अनावश्यक प्रकार का है, लेकिन पठनीय, तेज़, रेगेक्स का उपयोग नहीं करता है, और 99% सामान्य यूआरएल को हल करेगा (यदि आप 100% सुनिश्चित करना चाहते हैं, तो बस 6 का समर्थन करने के लिए स्विच ब्लॉक का विस्तार करें + डॉट्स, हालांकि मैंने कभी भी यूआरएल में कई बिंदु नहीं देखा है)।

आशा इस मदद करता है,

0

यहाँ एक और समारोह है कि प्रोटोकॉल संबंधित URL संभाल सकता है

<?php 
function getAbsoluteURL($to, $from = null) { 
    $arTarget = parse_url($to); 
    $arSource = parse_url($from); 
    $targetPath = isset($arTarget['path']) ? $arTarget['path'] : ''; 

    if (isset($arTarget['host'])) { 
     if (!isset($arTarget['scheme'])) { 
      $proto = isset($arSource['scheme']) ? "{$arSource['scheme']}://" : '//'; 
     } else { 
      $proto = "{$arTarget['scheme']}://"; 
     } 
     $baseUrl = "{$proto}{$arTarget['host']}" . (isset($arTarget['port']) ? ":{$arTarget['port']}" : ''); 
    } else { 
     if (isset($arSource['host'])) { 
      $proto = isset($arSource['scheme']) ? "{$arSource['scheme']}://" : '//'; 
      $baseUrl = "{$proto}{$arSource['host']}" . (isset($arSource['port']) ? ":{$arSource['port']}" : ''); 
     } else { 
      $baseUrl = ''; 
     } 
     $arPath = []; 

     if ((empty($targetPath) || $targetPath[0] !== '/') && !empty($arSource['path'])) { 
      $arTargetPath = explode('/', $targetPath); 
      if (empty($arSource['path'])) { 
       $arPath = []; 
      } else { 
       $arPath = explode('/', $arSource['path']); 
       array_pop($arPath); 
      } 
      foreach ($arTargetPath as $idx => $component) { 
       if ($component === '..' && !empty($arPath)) { 
        array_pop($arPath); 
       } else if ($component !== '.') { 
        array_push($arPath, $component); 
       } 
      } 
      $targetPath = implode('/', $arPath); 
     } 
    } 

    return $baseUrl . $targetPath; 
} 

// SAMPLES 
// Absolute path => https://www.google.com/doubleclick/ 
echo getAbsoluteURL('/doubleclick/', 'https://www.google.com/doubleclick/insights/') . "\n"; 
// Relative path 1 => https://www.google.com/doubleclick/studio 
echo getAbsoluteURL('../studio', 'https://www.google.com/doubleclick/insights/') . "\n"; 
// Relative path 2 => https://www.google.com/doubleclick/insights/case-studies.html 
echo getAbsoluteURL('./case-studies.html', 'https://www.google.com/doubleclick/insights/') . "\n"; 
// Relative path 3 => https://www.google.com/doubleclick/insights/case-studies.html 
echo getAbsoluteURL('case-studies.html', 'https://www.google.com/doubleclick/insights/') . "\n"; 
// Protocol relative url => https://www.google.com/doubleclick/ 
echo getAbsoluteURL('//www.google.com/doubleclick/', 'https://www.google.com/doubleclick/insights/') . "\n"; 
// Empty path => https://www.google.com/doubleclick/insights/ 
echo getAbsoluteURL('', 'https://www.google.com/doubleclick/insights/') . "\n"; 
// Different url => http://www.yahoo.com/ 
echo getAbsoluteURL('http://www.yahoo.com/', 'https://www.google.com') . "\n"; 
संबंधित मुद्दे