eregi()已弃用,需要使用preg_match替换。如何将以下语法转换为preg_match?假设以下是url并且它是必需的正则表达式模式:
$url = "https://james.wordpress.com/2013/05/13/my-product-final";
$urlregex = "^(https?|ftp)\:\/\/([a-zåäöÅÄÖæÆøØ0-9+!*(),;?&=\$_.-]+(\:[a-zåäöÅÄÖæÆøØ0-9+!*(),;?&=\$_.-]+)?@)?[a-zåäöÅÄÖæÆøØ0-9+\$_-]+(\.[a-zåäöÅÄÖæÆøØ0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-zåäöÅÄÖæÆøØ0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-zåäöÅÄÖæÆøØ0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-zåäöÅÄÖæÆøØ0-9+\$_.-]*)?\$";
以下代码正常运行:
if (eregi($urlregex, $url)) {
echo "Valid";
exit;
}
我尝试转换为'preg_match',但无法正常工作:
if (preg_match("#$urlregex#i", $url)) {
echo "Valid";
exit;
}
也尝试过这样:
if (preg_match('/'.$urlregex.'/i', $url))
答案 0 :(得分:1)
试试这个: -
$url = "https://james.wordpress.com/2013/05/13/my-product-final";
if (preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i",$url))
{
echo "Valid";
exit;
}
filter_var()函数还可以验证字符串是否为URL
答案 1 :(得分:1)
它不起作用,因为你用作分隔符的字符(即#
)存在于正则表达式中。
你可以:
#
:\#
答案 2 :(得分:0)
$website = "http://googl.com";
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "Invalid URL";
}