--- sites/all/modules/collapse_text/collapse_text.module	2010-01-07 11:45:58.000000000 -0500
+++ sites/all/modules/collapse_text/collapse_text.module.new	2010-01-07 11:44:56.000000000 -0500
@@ -10,7 +10,7 @@
 /**
  * Implementation of hook_filter_tips().
  */
-function collapse_text_filter_tips($delta, $format, $long = false) {
+function collapse_text_filter_tips($delta, $format, $long = FALSE) {
   if ($long) {
     return t('Enclose sections of text in [collapse] and [/collapse] to turn them into collapsible sections. If you use [collapse collapsed] and [/collapse], the section will start out collapsed. You may specify a title with [collapse title=some title] (or [collapse collapsed title=some title]). If no title is specified, the title will be taken from the first header (&lt;h1&gt;, &lt;h2&gt;, &lt;h3&gt;, ...) found. In the absence of a header, a default title is used.');
   }
@@ -61,31 +61,54 @@ function collapse_text_filter($op, $delt
  */
 function collapse_text_process($text) {
   // Per #259535 and #233877, add ability to specify title
-  // in collapse text. Thanks rivena, Justyn
-  //pull out old collapse statement
-
-  $pattern = "/\[collapse(.|\n|\r)*\[\/collapse\]/";
-  preg_match($pattern, $text, $matches);
-
-  $new_text = preg_replace_callback('/
-      (?:<p.*?>)?                # remove paragraph is right before and after
-      \[                         # look for an opening bracket
-         collapse                # followed by the word `collapse`
-         (\ collapsed)?          # followed by (optionally) a space and the word `collapsed` (captured)
-         (?:\ title=([^\]]*))?   # followed by (optionally) a space and a title, consisting of any
-                                 # characters except a close bracket (captured)
-      \]                         # followed by a closing bracket
-      (?:<\/p\s*>)?              # remove paragraph is right before and after
-      (.+?)                      # then capture as few characters as possible until
-      (?:<p.*?>)?                # remove paragraph is right before and after
-      \[\/collapse\]             # a closing "tag", which is a slash followed by `collapse` in brackets
-      (?:<\/p\s*>)?              # remove paragraph is right before and after
-    /smx',
-    "_collapse_text_replace_callback", $text);
-  //$text now contains our replacement text. We do not want to completely override the old text so we will search for the old
-  //replace it with the new and then return that text.
-
-  $text = str_replace($matches[0], $new_text, $text);
+  // in collapse text. Thanks rivena, Justyn
+  //pull out old collapse statement
+  //
+  
+  // Find pattern beginning with [collapse followed by 0 or more characters
+  // followed by 0 or more character followed by [/collapse]
+  
+  $pattern = "/\[collapse( collapsed)*([^\]]*)\]([^\[]*)\[\/collapse\]/";
+  
+  preg_match_all($pattern, $text, $matches);
+  
+  //If matches is provided, then it is filled with the results of search. 
+  //$matches[0] will contain the text that matched the full pattern, 
+  //$matches[1] will have the text that matched the first captured parenthesized subpattern, 
+  //and so on. 
+  
+  $totmatches = count($matches);
+  $totelements = count($matches, COUNT_RECURSIVE); 
+  $totmatches = ($totelements - $totmatches) / $totmatches;  // Total full matches
+  
+  for ($i = 0; $i < $totmatches; ++$i) {
+    // $matches[0][$i]) - Text that matched full pattern
+    // $matches[1][$i]) - Collapsed
+    // $matches[2][$i]) - Title
+    // $matches[3][$i]) - Inner Text
+    $title = trim($matches[2][$i]);
+    if (!empty($title) && strtolower(substr($title, 0, 6)) == 'title=') {
+      $title = substr($title, 6);
+    }
+    else {
+      $title = "";
+    }
+    $matches[2][$i] = $title;
+    
+    $repmatches[0] = $matches[0][$i];
+    $repmatches[1] = $matches[1][$i];
+    $repmatches[2] = $matches[2][$i];
+    $repmatches[3] = $matches[3][$i];
+  
+    $new_text = _collapse_text_replace_callback($repmatches);
+  
+    // $text now contains our replacement text. We do not want to completely override the old text 
+    // so we will search for the old replace it with the new and then return that text.
+    $text = str_replace($matches[0][$i], $new_text, $text);
+    $nbsp = '&nbsp;';
+    $space = ' ';
+    $text = str_replace($nbsp, $space, $text);
+  }
   return $text;
 }
 
@@ -113,12 +136,12 @@ function _collapse_text_replace_callback
   $render_array = array(
     '#type' => 'fieldset',
     '#title' => $title,
-    '#collapsible' => true,
+    '#collapsible' => TRUE,
     '#collapsed' => $collapsed,
   );
   $render_array['text_contents'] = array(
     '#type' => 'markup',
-    '#value' => '<div>' . $interior . '</div>',
+    '#value' => '<div>'. $interior .'</div>',
   );
   
   return drupal_render($render_array);
