Page 1 of 1
Determine if URL is valid
Posted: Sun Mar 13, 2011 4:01 pm
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.
Re: Determine if URL is valid
Posted: Wed Mar 30, 2011 8:51 am
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...)
Re: Determine if URL is valid
Posted: Wed Mar 30, 2011 11:17 am
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
}