Reputation: 63
How can I extract domain name from string like "AmericanSwan.com Indian Websites" ?
I am tried many options but none of them provide domain name.
Upvotes: 3
Views: 2292
Reputation: 8101
Use below Regular Expression for extracting domain name from string, Try:
$input = 'AmericanSwan.com Indian Websites';
preg_match_all('#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#si', $input, $result);
echo $result[0][0]; //$result will give list of all domain names of string, you can also loop through it
Output:
AmericanSwan.com
Upvotes: 4
Reputation: 8618
You should refer to this function: parse_url()
And this example: parse_url example
Upvotes: 0