trvr's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
34 (0.02 per day)
Joined:
09-March 09
Profile Views:
4,141
Last Active:
User is offline Aug 22 2012 10:58 PM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Mac
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
XBox
Your Car:
Volkswagen
Dream Kudos:
0

Latest Visitors

Icon   trvr has not set their status

Posts I've Made

  1. In Topic: URL Validation using Regex

    Posted 22 Aug 2012

    Thanks for the info!
  2. In Topic: URL Validation using Regex

    Posted 22 Aug 2012

    Wow I feel dumb now. I was using:
    if( $result == null) {
    // Do something
    }
    
    

    What is the difference between '==' and '===' in PHP? Is there ever a reason to use '=='?

    Also, thanks so much for your help I finally got it working. I also found the following function on stackoverflow.com that might be of use to others to perform a 'HEAD' request rather than a 'GET' request when using the get_headers() method, which from my understanding is less bandwidth intensive as it only downloads the header information rather than the whole page. This method is nice because it does not override the the standard stream_context, which I was also having trouble doing.
    function get_headers_with_stream_context($url, $context, $assoc = 0) {
        $fp = fopen($url, 'r', null, $context);
        $metaData = stream_get_meta_data($fp);
        fclose($fp);
    
        $headerLines = $metaData['wrapper_data'];
    
        if(!$assoc) return $headerLines;
    
        $headers = array();
        foreach($headerLines as $line) {
            if(strpos($line, 'HTTP') === 0) {
                $headers[0] = $line;
                continue;
            }
    
            list($key, $value) = explode(': ', $line);
            $headers[$key] = $value;
        }
    
        return $headers;
    }
    
    

    The method is called by doing the following:
    $context = stream_context_create(array('http' => array('method' => 'HEAD')));
    $headers = get_headers_with_stream_context($url, $context, 1);
    
    
  3. In Topic: URL Validation using Regex

    Posted 20 Aug 2012

    Unfortunately I am unable to get any part of your function to work. I broke the function down into sections to see what the issue was, and each section failed individually. I cannot seem to figure out what is causing everything to fail either because I have tried several deviations of valid URLs.

    function validate_url($url, $validateSyntax=true) 
    {
        if ($validateSyntax) {
            if ( !filter_var($url, FILTER_VALIDATE_URL) ) {
                return null; // Always returns null
            }
            $parts = parse_url($url);
            if (!$parts || !preg_match('#.+\.[a-z]{2,6}$#', $parts["host"])) {
                return null; // Always returns null
            }
        }
        $data = @get_headers($url);
        return ($data && preg_match('#HTTP/1\.(1|0) (2|3)\d{2}#', $data[0])); // Always returns false
    }
    
    

    Edit: I would love to just validate the URLs using the headers, but that part of the function does not seem to be working either unfortunately.
    preg_match('#HTTP/1\.(1|0) (2|3)\d{2}#', $data[0]) // Fails to validate valid HTTP headers.
    
    
  4. In Topic: URL Validation using Regex

    Posted 19 Aug 2012

    View PostAtli, on 19 August 2012 - 01:18 PM, said:

    It would be far easier to use the filter_var() or filter_input() functions instead.

    $url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL);
    
    if ($url === null) {
        echo "URL not sent by POST";
    }
    else if ($url === false) {
        echo "URL failed validation";
    }
    else {
        echo "URL is valid!";
    }
    
    

    I read about using that method, but I also read this method has a few holes in it. For example, I read that URLs with hyphens (e.g., this-is-a-url.com) would be invalidated as well as allow URLs like http://www to be validated. Although the latter would allow for URLs like http://localhost to be validated. This is why I have been trying to validate URLs using regex.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Years Programming:
3
Programming Languages:
Learning Java, Learning C++

Contact Information

E-mail:
Private
Website URL:
Website URL  http://

Friends

trvr hasn't added any friends yet.

Comments

trvr has no profile comments yet. Why not say hello?