$re = '/(?<!^)[ \t]*\r?\n(?=\$)/m';
$str = '$index 57320
$title The vertex-cover polynomial of a graph
$time 1988
$abstract In this paper we define the vertex-cover...
$index 57321
$title Locating stations on rapid transit lines
$time 1978
$index 57322
$title Fast heuristics for large scale covering-location problems
$time 1998
$abstract We propose fast heuristics for large scale...
$index 57323
$title Efficient vector processing on dataflow supercomputer SIGMA-1
$time 2001
$abstract Efficiency in vector handling is the key to obtaining high... ';
$subst = ",";
$result = preg_replace($re, $subst, $str);
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