PHP: Как разрешить относительный URL

Пример: в файле web.xml тег

<context-param>
        <param-name>chatpropertyfile</param-name>
        <!--  Name of the chat properties file. It contains the name and description                   of rooms.-->     
        <param-value>chat.properties</param-value>
    </context-param>

И chat.properties вы можете объявить свои свойства следующим образом:

Для Ex:

Jsp = Discussion about JSP can be made here.
Java = Talk about java and related technologies like J2EE.
ASP = Discuss about Active Server Pages related technologies like VBScript and JScript etc.
Web_Designing = Any discussion related to HTML, JavaScript, DHTML etc.
StartUp = Startup chat room. Chatter is added to this after he logs in.
26
задан Paul Tarjan 7 August 2009 в 18:25
поделиться

3 ответа

другие инструменты, которые уже связаны на странице, связанной в комментарии 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
ответ дан qdinar 25 September 2019 в 08:25
поделиться

Возможно, эта статья может помочь?

http: // nashruddin.com/PHP_Script_for_Converting_Relative_to_Absolute_URL

Редактировать: приведенный ниже код для удобства

<?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;
    }
?>
8
ответ дан Taco de Wolff 25 September 2019 в 08:25
поделиться

Еще одно решение, если вы уже используете GuzzleHttp .

Это решение основано на внутреннем методе GuzzleHttp\Client.

use GuzzleHttp\Psr7;

function resolve(string $uri, ?string $base_uri): string
{
    $uri = Psr7\uri_for($uri);

    if (isset($base_uri)) {
        $uri = Psr7\UriResolver::resolve(Psr7\uri_for($base_uri), $uri);
    }

    // optional: set default scheme if missing
    $uri = $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri;

    return (string) $uri;
}
1
ответ дан Johannes Buchholz 25 September 2019 в 08:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: