从给定的URL更改URL扩展名

时间:2014-06-02 10:21:21

标签: php regex

有没有办法接受网址并将其更改为.com

例如,如果用户要提交www.example.in,我想检查网址是否有效,并将其更改为www.example.com。我已经构建了一个正则表达式检查程序,可以检查URL是否有效,但我不完全确定如何检查给定的扩展名是否有效,然后将其更改为.com

编辑:要明确我实际上并没有使用这些网址。我将它们作为用户输入提交到表单中,并且只是存储它们。这些是我想在存储之前对URL做的功能,即全部。

编辑2:一个让这个更清晰的例子 -

$url = 'www.example.co.uk'
$newurl = function($url);
echo $newurl

将产生输出

 www.example.com

4 个答案:

答案 0 :(得分:1)

您是否在服务器端寻找类似的内容来替换要转换为.coms的所选TLD列表?

<?php
    $url = "www.example.in";
    $replacement_tld = "com";

    # array of all TLDs you wish to support
    $valid_tlds = array("in","co.uk");
    # possible TLD source lists
    #     http://data.iana.org/TLD/tlds-alpha-by-domain.txt
    #     https://wiki.mozilla.org/TLD_List



    # from http://stackoverflow.com/a/10473026/723139
    function endsWith($haystack, $needle)
    {   
        $haystack = strtolower($haystack);
        $needle = strtolower($needle);
        return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
    }
    foreach($valid_tlds as $tld){
        if(endsWith($url, $tld))
        {
            echo substr($url, 0, -strlen($tld)) . $replacement_tld . "\n";
            break;
        }
    }
?>

答案 1 :(得分:0)

问题并不完全清楚,我假设您希望在PHP上创建这个逻辑。

这是解析此类字符串的有用功能:

function parseUrl ( $url )
{
    $r = "^(?:(?P<scheme>\w+)://)?";
    $r .= "(?:(?P<login>\w+):(?P<pass>\w+)@)?";
    $r .= "(?P<host>(?:(?P<subdomain>[\w\.\-]+)\.)?" . "(?P<domain>\w+\.(?P<extension>\w+)))";
    $r .= "(?::(?P<port>\d+))?";
    $r .= "(?P<path>[\w/]*/(?P<file>\w+(?:\.\w+)?)?)?";
    $r .= "(?:\?(?P<arg>[\w=&]+))?";
    $r .= "(?:#(?P<anchor>\w+))?";
    $r = "!$r!";

    preg_match( $r, $url, $out );

    return $out;
}

您可以解析URL,验证它,然后从结果数组中重新创建,替换您想要的任何内容。

如果您想练习正则表达式并创建自己的模式 - this site will be best place to do it.

如果您的目标是将用户从一个网址路由到另一个网址或更改URI样式,那么您需要使用mod重写。

实际上,在这种情况下,您将最终配置您的Web服务器,可能是虚拟主机,因为它只会路由列出的域(停放在服务器上的域)。

答案 2 :(得分:0)

  1. 使用文本编辑器(如记事本)创建空文本文件,并将其另存为htaccess.txt。
  2. 301(永久)重定向:永久性地将整个站点指向不同的URL。这是最常见的重定向类型,在大多数情况下都很有用。在此示例中,我们将重定向到&#34; mt-example.com&#34;域:

    # This allows you to redirect your entire website to any other domain
    Redirect 301 / http://mt-example.com/
    

    302(临时)重定向:将整个站点指向其他临时URL。当您拥有临时目标网页并计划在以后切换回主要目标网页时,这对于搜索引擎优化目的非常有用:

    # This allows you to redirect your entire website to any other domain
    Redirect 302 / http://mt-example.com/
    

    有关详情:http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F

答案 3 :(得分:0)

验证PHP中的URL您可以使用 filter_var()

filter_var($ url,FILTER_VALIDATE_URL))

然后获取顶级域(TLD)并将其替换为.com,您可以使用以下功能:

 $url="http://www.dslreports.in";
 $ext="com";
 function change_url($url,$ext)
 {
    if(filter_var($url, FILTER_VALIDATE_URL)) {
    $tld = '';
    $url_parts = parse_url( (string) $url );
    if( is_array( $url_parts ) && isset( $url_parts[ 'host' ] ) )
      {
        $host_parts = explode( '.', $url_parts[ 'host' ] );
        if( is_array( $host_parts ) && count( $host_parts ) > 0 )
           {
          $tld = array_pop( $host_parts );
           }
       }

        $new_url=  str_replace($tld,$ext,$url);
        return $new_url;
   }else{
    return "Not a valid URl";
   }
}

 echo change_url($url,$ext);

希望这有帮助!

相关问题