Musings of a Philadelphia Physician who has served the community for six decades

Return to Home

Related Topics

Website Development
The website technology supporting Philadelphia Reflections is PHP, MySQL and DHTML. The web hosting service is Internet Planners. The development of this website has provided an opportunity to learn new technology, to try out different techniques for getting noticed by the search engines and the trials and tribulations of dealing with malicious hackers and spammers who range from the annoying to the abusive. This collection of articles documents some of our experiences and we hope that people surfing the web looking for solutions to problems we've encountered will benefit.

Regex URL Matching

On this site we check for the existence of a URL whenever an entry is updated

There are two key technologies at work

  • A PHP function that checks whether a URL is valid (thanks to marufit at gmail dot com in the PHP Manual)

  • Regex (regular expression) in a preg_replace_callback routine; this one is mine, all mine

function url_exists($url) 
// checks whether a URL actually exists on the Internet
$handle   = curl_init($url);
if (false === $handle)
    return false;
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true); 
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
$connectable = curl_exec($handle);
return $connectable;

function aExists($matches)
// function called by preg_replace_callback
// $matches[0] is the complete match
// $matches[1] the match for the first subpattern
//	enclosed in '(...)' and so on

// checks to see if a regular link exists
// something similar is done for img src= also

$srcURL = $matches[3];
if (url_exists($srcURL)) {do something; return "";}  
else {do something else; return "";}

$foo = preg_replace_callback(
            '/(.*?)(<a .*?href=")([^"]*)("[^>]*>)(.*?)(<\/a>)/i',

(my thanks to for HTML encoding)


Please Let Us Know What You Think

(HTML tags provide better formatting)

Because of robot spam we ask you to confirm your comment: we will send you an email containing a link to click. We apologize for this inconvenience but this ensures the quality of the comments. (Your email will not be displayed.)
Thank you.