Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Regex for extracting subdomains of arbitrary domains
    primarykey
    data
    text
    <p>I want to extract the subdomain and domain part for domains with arbitrary top level extensions.</p> <p>Thus:</p> <p>sub1.domain1.com --> Extract subdomain=sub1, domain=domain1.com</p> <p>sub2.domain2.co.in --> Extract subdomain=sub2, domain=domain2.co.in</p> <p>sub3.domain3.co.uk --> Extract subdomain=sub3, domain=domain3.co.uk</p> <p>sub4.domain4.us --> Extract subdomain=sub4, domain=domain4.us</p> <p>mydomain.com --> Extract subdomain="", domain=mydomain.com</p> <p>mydomain.co.in --> Extract subdomain="", domain=mydomain.co.in</p> <p>I am bit confused about how to handle TLDs like co.in/co.uk etc. I could do this using brute force way by measuring if the last 5 characters have a DOT (.) in them, but thinking if there is a regex way to do this.</p> <hr> <p>NOTE 1: As TToni pointed out, there can be ambiguities. However, I will put some constraints:</p> <p>1) The "Domain name" part (without the extension) --> will be at-least 4 characters.</p> <p>2) The TLD extension part (.com, co.in, .us, etc) will either have a single <em>DOT</em> or if it has two <em>DOTS</em>, then the penultimate part (sub TLD) will have atmost 3 characters.</p> <p>I have a feeling that these constraints will make the problem unambigious and solvable using regex.</p> <p>(Also, assume "www." has been stripped out already).</p> <hr> <p>NOTE 2: </p> <p>Example of above constraints</p> <p>sub.dom.in --> domain="sub.dom.in"</p> <p>sub.dom1.in --> domain="dom1.in", subdomain="sub"</p> <p>This may sound buggy, but the reason is - I want this for my internal purposes, and all my domains have atleast 4 characters in them, AND, all extensions have either single DOT or the penultimate part is at-max 3 characters.</p> <hr> <p>NOTE 3: I have a feeling I might make mistakes by using regex for this. Hence thinking of doing the string search way.</p> <p>regards,</p> <p>JP</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload