$text"; return $link; } else { // return the full matched string as it was. // this is to leave alone legitimate entities such as "&". return $matches[0]; } } $path = dirname(dirname(__FILE__)); $list = new RecursiveIteratorIterator( new RecursiveDirectoryIterator("$path/source/manual"), RecursiveIteratorIterator::SELF_FIRST ); foreach ($list as $name => $item) { if (substr($name, -4) == '.xml') { $xml = file_get_contents($name); $xml = preg_replace_callback('/\&(.*?)\;/s', "xlink_entities", $xml); file_put_contents($name, $xml); } }