diff --git a/modules/filter.module b/modules/filter.module
index 0e345cce666382d9d0b0c84070154771af6cb6ea..42204554313436f315dd5484f6866879fce112b2 100644
--- a/modules/filter.module
+++ b/modules/filter.module
@@ -1135,9 +1135,9 @@ function _filter_xss_split($m, $store = FALSE) {
   }
 
   // Is there a closing XHTML slash at the end of the attributes?
-  $attrlist = preg_replace('%(\s?)/\s*$%', '\1', $attrlist);
-  // in PHP 5.1.0 we could count the changes, currently we need this
+  // In PHP 5.1.0+ we could count the changes, currently we need a separate match
   $xhtml_slash = preg_match('%\s?/\s*$%', $attrlist) ? ' /' : '';
+  $attrlist = preg_replace('%(\s?)/\s*$%', '\1', $attrlist);
 
   // Clean up attributes
   $attr2 = implode(' ', _filter_xss_attributes($attrlist));
diff --git a/modules/filter/filter.module b/modules/filter/filter.module
index 0e345cce666382d9d0b0c84070154771af6cb6ea..42204554313436f315dd5484f6866879fce112b2 100644
--- a/modules/filter/filter.module
+++ b/modules/filter/filter.module
@@ -1135,9 +1135,9 @@ function _filter_xss_split($m, $store = FALSE) {
   }
 
   // Is there a closing XHTML slash at the end of the attributes?
-  $attrlist = preg_replace('%(\s?)/\s*$%', '\1', $attrlist);
-  // in PHP 5.1.0 we could count the changes, currently we need this
+  // In PHP 5.1.0+ we could count the changes, currently we need a separate match
   $xhtml_slash = preg_match('%\s?/\s*$%', $attrlist) ? ' /' : '';
+  $attrlist = preg_replace('%(\s?)/\s*$%', '\1', $attrlist);
 
   // Clean up attributes
   $attr2 = implode(' ', _filter_xss_attributes($attrlist));