$re = '/<a class=.*?href=".*?([^\/]+)\.txt">.*?<\/a>/';
$str = '<a class="xref" href="../Test_Topic_1/Test_Topic_1.txt">Test Topic 1</a>';
$subst = "<ac:link><ri:page ri:content-title=\"\1\" \/><\/ac:link>";
$result = preg_replace($re, $subst, $str, 1);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php