Determine if URL is valid

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
jitsking
Forum Newbie
Posts: 2
Joined: Sun Mar 13, 2011 3:48 pm

Determine if URL is valid

Post by jitsking »

I'd like to create a script that will determine if a URL is valid. The way I need for my script to determine this is to look for a specific Div ID on the web page named "dialog" and check the display style property. If it's equal to "none" then it's valid. Otherwise, it's not valid. I seen ways to do this with JavaScript but wondering if it can be done with PHP since it seems like it might be difficult to pass a variable from Javascript to PHP.
skylark2
Forum Commoner
Posts: 32
Joined: Wed Jun 16, 2010 6:00 am

Re: Determine if URL is valid

Post by skylark2 »

Addressing your last point - the way I pass variables from Javascript to php is to create an input field with type="hidden" in the form and set its value in the javascript. Then it gets passed just like all the other form fields when the form is submitted by the user - or you can submit the form direct from the javascript if you want.

There are doubtless more sophisticated ways to handle large quantities of data, but I've only ever needed one variable, and it sounds as if you might need the same.

(Yay! I think this is my first ever answer. Normally I just ask questions...)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Determine if URL is valid

Post by Jonah Bron »

Take a look at the DOM library.

http://php.net/dom

Here's how it might be done (untested):

Code: Select all

// Create the DOM document
$document = new DOMDocument();

// Use loadHTML to load the HTML without throwing errors for bad syntax
$document->loadHTML(/* HTML content here.  Alternatively, use loadHTMLFile() */);

// Create new XPath querying object
$xpath = new DOMXPath($document);

// Execute query to search for a <div> with the ID 'dialog', and get the first result
$div = $xpath->query('//div[@id=\'dialog\']')->item(0);

// Extract the style attribute from the tag
$style = $div->getAttribute('style');

// Check display value
if (preg_match('/display\:[^\;]*?none/i', $style)) {
    // is valid
} else {
    // not valid
}
Post Reply