4

次のphp変数があります

$currentUrl

この php 変数は、現在の URL ページを返します。例: 以下を返します。

http://example.com/test-category/page.html?_ore=norn&___frore=norian

この url リンクを取得し、「 .html 」の後のすべてを削除し、きれいな url リンクを返すことができる php コードを使用できます。たとえば、次のようになります。

http://example.com/test-category/page.html

これは、新しい変数$clean_currentUrlで返されます

4

3 に答える 3

13

PHPでparse_url()

<?php 
$url = "http://example.com/test-category/page.html?_ore=norn&___frore=norian";
$url = parse_url($url);

print_r($url);
/*
Array
(
    [scheme] => http
    [host] => example.com
    [path] => /test-category/page.html
    [query] => _ore=norn&___frore=norian
)
*/
?>

次に、値から目的の URL を作成できます。

$clean_url = $url['scheme'].'://'.$url['host'].$url['path'];
于 2013-06-23T00:08:37.647 に答える
1
$parts = explode('?', $currentUrl);
$url = $parts[0];
于 2013-06-23T00:08:21.927 に答える
1

このようなもの:

<?php
$currentUrl = 'http://example.com/test-category/page.html?_ore=norn&___frore=norian';

preg_match('~http:\/\/.*\.html~', $currentUrl, $matches);
print_r($matches);

以下のアミグラのコメントを参照してください。そのケースを処理するには、正規表現を変更します。

<?php
$currentUrl = 'http://example.com/test-category/page.html?_ore=norn&___frore=norian';

preg_match('~(http:\/\/.*\..+)\?~', $currentUrl, $matches);
print_r($matches);
于 2013-06-23T00:09:59.573 に答える