Check for domain extension with PHP or JS

主宰稳场 提交于 2019-12-11 13:29:34

问题


First of all I am a php beginner so please be as specific as possible. Basically I want a php/javascript that does the following:

if the domain has a .de extension then add the text "this is a german site" else add the text "this is not a german site"

it's also ok if you do it with the lang="de-DE" instead of the domain extension.


回答1:


To get the domain extension use php's pathinfo

$extension = pathinfo($_SERVER['SERVER_NAME'], PATHINFO_EXTENSION);

if($extension == "de")
{
echo "this is a german site";
}

Also see: $_SERVER




回答2:


In PHP, try something like this:

<?php
//~ $domain = $_SERVER['HTTP_HOST'];
$domain = "domain.de";

if (preg_match('/(.*?)\.de$/', $domain)) {
    echo "is german";
} else {
    echo "is not german";
};
?>

Greatings.




回答3:


var isDE="";
var extension=location.hostname.split(".");
extension=extension[extension.length-1];
if (extension=="de") isDE="this is a german site".
//do whatever you need with isDE, e.g. document.write(isDE);


来源:https://stackoverflow.com/questions/10604829/check-for-domain-extension-with-php-or-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!