代码之家  ›  专栏  ›  技术社区  ›  Dan YOU

URL验证php

  •  0
  • Dan YOU  · 技术社区  · 16 年前

    任务是查找字符串是否以http://或https://或ftp开头://

    $regex=“(https?| ftp):/)?”;

    4 回复  |  直到 16 年前
        1
  •  3
  •   Thiago Belem    16 年前

    您需要在RegExp()周围使用分隔符(/)

    // Protocol's optional
    $regex = "/^((https?|ftp)\:\/\/)?/";
    // protocol's required
    $regex = "/^(https?|ftp)\:\/\//";
    
    if (preg_match($regex, 'http://www.google.com')) {
        // ...
    }
    

    http://br.php.net/manual/en/function.preg-match.php

        2
  •  1
  •   robbo    16 年前

    if (strpos($url, 'http://')  === 0 ||
        strpos($url, 'https://') === 0 ||
        strpos($url, 'ftp://')   === 0)
    {
        // do magic
    }
    
        3
  •  0
  •   K Prime    16 年前

    你需要: preg_match ('#((https?|ftp)://)?#', $url)

    这个 # / ,这对URL更方便

        4
  •  0
  •   GZipp    16 年前

    这样地:

    $search_for = array('http', 'https', 'ftp');
    $scheme = parse_url($url, PHP_URL_SCHEME);
    if (in_array($scheme, $search_for)) {
        // etc.
    }
    
    推荐文章