كود PHP لاستخلاص الدومين الرئيسي " domain.com " من الدومين الفرعي " sub.domain.com "
وجدت نفس الكود بالضبط لكن ليس بأسمك
عموماً هناك حل اسهل للنطاقات ليس للـ ip او اسماء العناوين المحلية كـ
او
اظنني قمت بالتعديل عليه مسبقاً ليدعم الـ Ip و الاسماء. لكن لا اذكر اين حفظت الدالة.
$url = "
http://test .test2.reashidf.com.sa/hello.php";
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
echo get_domain($url);
وجدته هنا:
وتستطيع اختباره على الامثلة التي طرحها صاحب الكود الذي وضعته كا التالي:
function get_domains($domains){
for($i =0; $i < count($domains); $i++){
$result = (!get_domain($domains[$i]))? '<span style="color:red">NULL</span>': get_domain($domains[$i]);
echo '<b>Given</b>: '. $domains[$i]. ' <b>Result</b>: '. $result . '<br>';
}
}
$urls = array(
'
http://www.example.com ', ' http://example.com ', ' http://example.com .br', ' http://www.example.com .br', ' http://www.example.gov.br ', ' http://localhost ', ' http://www.localhost ', ' http://subdomain.localhost ', ' http://www.subdomain.exampl... ', ' http://subdomain.example.com ', ' http://subdomain.example.com .br', ' http://www.subdomain.exampl... .br', ' http://www.subdomain.exampl... ', ' http://subdomain.example.bi... ', ' http://subdomain.example.net ', ' http://www.subdomain.exampl... ', ' http://www.subdomain.exampl... ', ' http://subdomain.example.co.kr ', ' http://example.co.kr ', ' http://example.jobs ', ' http://www.example.jobs ', ' http://subdomain.example.jobs ', ' http://insane.subdomain.exa... ', ' http://insane.subdomain.exa... ', ' http://www.doubleinsane.sub... ', ' http://www.subdomain.exampl... ', ' http://test ', ' http://www.test ', ' http://subdomain.test ', ' http://www.detran.sp.gov.br ', ' http://www.mp.sp.gov.br ', ' http://ny.library.museum ', ' http://www.ny.library.museum ', ' http://ny.ny.library.museum ', ' http://www.library.museum ', ' http://info.abril.com.br ', ' http://127.0.0.1 ', ' http:/ ::1' );
get_domains($urls);
التعليقات