在codeigniter中有效的URL检查表单验证

时间:2015-01-01 12:55:29

标签: php regex codeigniter

以下是我在CI中使用url验证函数的代码,这是回调函数

function valid_url($facebook)
    {
        $pattern = "/^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/";
        if (!preg_match($pattern, $facebook))
        {
            return FALSE;
        }

        return TRUE;
    }     

我收到此错误

Message: preg_match() [function.preg-match]: Unknown modifier '|'

1 个答案:

答案 0 :(得分:1)

试试这个

        function validate_url($url) {
        $data = trim($url);
        $data = stripslashes($url);
        $data = htmlspecialchars($url);
        return $url;


     // check address syntax is valid or not(this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)) {
        return FALSE;
        } else {
    return TRUE;
    }
     } 
  $check =  validate_url($url);