preg_match to domain.tld
How can i get the strings that only contains domain.tld?
if (preg_mat开发者_开发知识库ch('^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$', $domain))
echo "domain.tld found!";
but for the regex ^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$
, sub domains are also okay. But i just want to check "domain.tld". What regex should i use for it?
Thank you.
if (preg_match('/^[-a-z0-9]+\.[a-z]{2,6}$/', strtolower($domain)))
echo "domain.tld found!";
would be the obvious answer. You could always be more specific on the tld part if you wanted to remove false positives:
if (preg_match('/^[-a-z0-9]+\.[a-z][a-z]|biz|cat|com|edu|gov|int|mil|net|org|pro|tel|aero|arpa|asia|coop|info|jobs|mobi|name|museum|travel$/', strtolower($domain)))
echo "domain.tld found!";
If you wanted to spell out the ccTLDs also, here's the current list:
if (preg_match('/^[-a-z0-9]+\.a[cdefgilmnoqrstuwxz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmoz]|e[cegrstu]|f[ijkmor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdeghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eosuw]|s[abcdeghijklmnortuvyz]|t[cdfghjklmnoprtvwz]|u[agksyz]|v[aceginu]|w[fs]|y[et]|z[amw]|biz|cat|com|edu|gov|int|mil|net|org|pro|tel|aero|arpa|asia|coop|info|jobs|mobi|name|museum|travel$/', strtolower($domain)))
echo "domain.tld found!";
I'd also accept the internationalized TLDs
xn--p1ai|xn--fiqs8s|xn--fiqz9s|xn--o3cw4h|xn--wgbh1c|xn--zckzah|xn--0zwm56d|xn--deba0ad|xn--g6w251d|xn--j6w193g|xn--kprw13d|xn--kpry57d|xn--pgbs0dh|xn--jxalpdlp|xn--kgbechtv|xn--fzc2c9e2c|xn--ygbi2ammx|xn--9t4b11yi5a|xn--mgbaam7a8h|xn--mgbayh7gpa|xn--80akhbyknj4f|xn--xkc2al3hye2a|xn--11b5bs3a9aj6g|xn--mgberp4a5d4ar|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a
or at least xn--[a-z0-9]+
.
So those plus .arpa
give
if (preg_match('^[-a-z0-9]+\.a[cdefgilmnoqrstuwxz]|b[abdefghijmnorstvwyz]|c[acdfghiklmnoruvxyz]|d[ejkmoz]|e[cegrstu]|f[ijkmor]|g[abdefghilmnpqrstuwy]|h[kmnrtu]|i[delmnoqrst]|j[emop]|k[eghimnprwyz]|l[abcikrstuvy]|m[acdeghklmnopqrstuvwxyz]|n[acefgilopruz]|om|p[aefghklmnrstwy]|qa|r[eosuw]|s[abcdeghijklmnortuvyz]|t[cdfghjklmnoprtvwz]|u[agksyz]|v[aceginu]|w[fs]|y[et]|z[amw]|biz|cat|com|edu|gov|int|mil|net|org|pro|tel|aero|arpa|asia|coop|info|jobs|mobi|name|museum|travel|arpa|xn--[a-z0-9]+$', strtolower($domain)))
You want to find a specific text domain.tld
while the regex with combination of A-Za-z0-9
character classes will match just about any text. Why don't you simply do:
if (strpos('domain.tld', $domain) !== false) {
echo "domain.tld found!";
}
to extract http://www.example.com from
something http://www.example.com/page/blabla.html some thingggggg
<?php
preg_match('/((http:||https:)\/\/.*?)[\/||\n||\s]/s', $f,$n);
echo $n[1];
?>
精彩评论