我發現了網站驗證。但後來我需要做一些修改,並添加特殊的社交網絡(臉譜,推特,plus.google)驗證。終極網址和社交網絡網址驗證
這是我的本錢:
function isValidUrl($url,$media) {
$url= strtolower($url);
// Scheme
$urlregex = "^(https?)\:\/\/";
// User and Pass (optional)
if (!isset($media)) {
$urlregex .= "([A-Za-z0-9+!*(),;?&=\$_.-]+(\:[A-Za-z0-9+!*(),;?&=\$_.-]+)[email protected])?";
}
// Hostname
if (isset($media)) {
if ($media == 'fb') { $urlregex .= "([facebook]+\.)"; }
else if ($media == 'gplus') { $urlregex .= "([plus\.google]+\.)"; }
else if ($media == 'twitter') { $urlregex .= "([twitter]+\.)"; }
} else {
$urlregex .= "([A-Za-z0-9+\$_-]+\.)";
}
$urlregex .= "*(?:[A-Za-z]{2}|com";
if (!isset($media)) {
$urlregex .= "|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum|cat|coop|int|pro|tel|travel|xxx";
}
// Hostname End
$urlregex .= ")";
// Port/Path (optional)
$urlregex .= "(\:[0-9]{2,5})?";
$urlregex .= "(\/([A-Za-z0-9+\$_-]\.?)+)*\/?";
// Query
$urlregex .= "(\?[A-Za-z+&\$_.-][A-Za-z0-9;:@/&%=+\$_.-]*)?";
// Anchor
$urlregex .= "(#[A-Za-z_.-][A-Za-z0-9+\$_.-]*)?\$^";
return preg_match($urlregex,$url);
}
簡單的網站是好的驗證,但社交網絡是不是我想要的方式。 例如http://facebook.com
是有效的,但我需要有效的URL像http://facebook.com/something
,並使第一個變得無效(http://twitter.com
和http://plus.google.com
相同)。 http://plus.google.com
驗證不起作用,它允許http://plusgoogle.com
和其他融合。
我想什麼改變/添加:
1)修正社交網絡(臉譜,gplus,微博)驗證如上所述;
2)中,也允許無協議http://
或與他們www.
兩個URL,因此使鏈接http://stackoverflow.com
成爲允許的方式http://www.stackoverflow.com
,stackoverflow.com
和www.stackoverflow.com
。
編輯:爲了說清楚,我把這個函數叫做下面的方法。
$error = false;
// For simple URL
$url = $_POST['url'];
if (!isValidURL($url,NULL)) { $error = true; }
// For Facebook URL
$fbpage = $_POST['fbpage'];
if (!isValidURL($fbpage,'fb')) { $error = true; }
// For Twitter URL
$twitterpage = $_POST['twitterpage'];
if (!isValidURL($twitterpage,'twitter')) { $error = true; }
// For Google Plus URL
$gpluspage = $_POST['gpluspage'];
if (!isValidURL($gpluspage,'gplus')) { $error = true; }
你想要「驗證」開始?你想接受任何*有效的URL *嗎?只有指定數量的網站的網址?請更清楚地描述你的背景和目的。 – deceze
「Not Social」網址驗證已生效。所有我想改變的東西都可以在這個問題的最後一段找到。 – Hypn0tizeR
是的,但不應該允許「plusgoogle.com」...但它是一個完全有效的網址。另外,你的解釋並不完全清楚,這可能是由於你的英語。關於*爲什麼*您提供了更好的信息的更多信息我們可以幫助您。 – deceze