*/ /** * Convert line breaks into
in an intelligent fashion. * * Policy * * Do not touch original
tags that are already in the data * * Allow duplicate line breaks (eg:

) * * Do not touch line break inside of a HTML tag (line break between < and >) * * Do not touch line break right after HTML tag * * Do not convert line breaks in the following tag block * ... ,
...
,
 ... 
* ... , , * * Enclose the entire text with

....

tag */ function _filter_brbr($text) { // 1. cross-platform line break to UNIX line breaks $text = str_replace(array("\r\n", "\r"), "\n", $text); // 2. change line break inside of HTML tags to temporary line break markers // so that step 5 does not change them to
$text = preg_replace('/<([^>\n]*)\n([^<\n]*)>/i', "<$1##!!LB!!##$2>", $text); // 3a. change line break right after the HTML tags to temporary line break // markers so that step 5 does not change them to
// $text = preg_replace('/>(\s*?)\n/', ">$1##!!LB!!##", $text); // do not convert line break to
after the following tags (start/end tags) // line breaks after all the other tags are converted to
$text = preg_replace('/<(\/*?\s*?)(th|td|tr|thead|tbody|tfoot|table|address|li|ul|ol|dt|dd|dl|div|span|caption|colgroup|p|br|blockquote|hr|h[1-6]|img)(\s*?.*?)>(\s*?)\n/i', "<$1$2$3>$4##!!LB!!##", $text); // converts line break after HTML tag to
if the next line does not start with // HTML tag // NOTE: Problem!!! // The following case will add
before the 1st line of contents! // // 1. First item // 2. Second item // // // $text = preg_replace('/>(\s*?)\n(\s*?)(<)/', ">$1##!!LB!!##$2$3", $text); // 3b. change line break right after pseudo tag to temporary line break // markers so that step 5 does not change them to
$text = preg_replace('/\](\s*?)\n/', "]$1##!!LB!!##", $text); // 4. change the
in the original data to temporary br tag marker // so that step 6 does not remove them $text = preg_replace('/<\s*br\s*\/?>/i', "##!!BR!!##", $text); // 5. insert
for each line break $text = str_replace("\n", "
\n", $text); // 6. do not insert
for the line breaks inside the //
, , , , 
  //    
, tag block $text = preg_replace('/()(.*?)<\/object>/ise', "_clr_br('$0')", $text); $text = preg_replace('/()(.*?)<\/form>/ise', "_clr_br('$0')", $text); $text = preg_replace('/()(.*?)<\/pre>/ise', "_clr_br('$0')", $text); $text = preg_replace('/()(.*?)<\/code>/ise', "_clr_br('$0')", $text); $text = preg_replace('/()(.*?)<\/style>/ise', "_clr_br('$0')", $text); $text = preg_replace('/()(.*?)<\/script>/ise', "_clr_br('$0')", $text); // do not insert
for the line breaks inside the PHP code $text = preg_replace('/(<\?php)(.*?)\?>/ise', "_clr_br('$0')", $text); // 7. Change the temporary line break markers back to line breaks $text = str_replace("##!!LB!!##", "\n", $text); // 8. Change the temporary br tag marker back to the BR tag $text = str_replace("##!!BR!!##", "
", $text); // 9. finally, create a paragraph block that covers all the text data $text="

\n".$text."

\n"; return $text; } function _clr_br($str){ $str = str_replace("
","",$str); $str = str_replace('\"','"',$str); return $str; } /** * Implements hook_filter_info() */ function brbr_filter_info() { $filters['brbr'] = array( 'title' => t('brbr line break converter'), 'description' => t('Converts line breaks to <br /> tags. It allows multiple line breaks and converts them into multiple <br /> tags.'), 'process callback' => 'brbr_filter_process', 'tips callback' => 'brbr_filter_tips', ); return $filters; } /** * Implementation of hook_filter() */ function brbr_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) { if (empty($text)) return $text; //--DEBUG // brbr_debugout("----before filter----"); // brbr_debugout($text); $text = _filter_brbr($text); //--DEBUG // brbr_debugout("----after filter----"); // brbr_debugout($text); return $text; } /** * Implements hook_filter_FILTER_tips() */ function brbr_filter_tips($filter, $format, $long) { return t('Converts line breaks to <br /> tags. It allows multiple line breaks and converts them into multiple <br /> tags.'); } /** * Debug function */ function brbr_debugout($str) { // return; // for production $outfile = "/tmp/brbr.txt"; if(is_array($str) || is_object($str)) { error_log(print_r($str, TRUE) . "\n", 3, $outfile); } else { error_log($str . "\n", 3, $outfile); } }