Code: Select all
function create_link($matches) {
// link_id should be an auto-increment field
$insert = mysql_query(sprintf("INSERT INTO links ( link_url ) VALUES ( '%s' )", $matches[1]));
return '<a href="' . mysql_insert_id() .'">'. $matches[2] .'</a>';
}
// File name, could be a url
$file = 'file.html';
if (file_exists($file)) {
$content = file_get_contents($file);
// Regex replace string
$pattern = '/<a href="([^"]+)">([^<]+)<\/a>/s';
$content = preg_replace_callback($pattern, 'create_link', $content);
echo $content;
}