Vijay G
Vijay G

Reputation: 63

Extract domain name from string using PHP

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

Answers (2)

Dhara Parmar
Dhara Parmar

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

Indrasis Datta
Indrasis Datta

Reputation: 8618

You should refer to this function: parse_url()

And this example: parse_url example

Upvotes: 0

Related Questions