- preg_replace: documentation ( source)
<?php
$re = '/(.*?)(\[\w{3}=\')(\w+\.\w+)(\'\])(.*?)(\[\w{3}=\')(\w+\.\w+).*/s';
$str = 'Hello, view this pdf [PDF=\'hello.pdf\']
and then view this PDF [PDF=\'goodmorning.pdf\']';
$subst = '$0';
$result = preg_replace($re, $subst, $str);
echo $result;