Include preg_last_error() in error message when preg_* fails
[lhc/web/wiklou.git] / includes / MagicWord.php
index 833f852..17c7cd4 100644 (file)
@@ -941,6 +941,7 @@ class MagicWordArray {
         *
         * @param string $text
         *
+        * @throws Exception
         * @return array
         */
        public function matchAndRemove( &$text ) {
@@ -950,12 +951,25 @@ class MagicWordArray {
                        if ( $regex === '' ) {
                                continue;
                        }
-                       preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
-                       foreach ( $matches as $m ) {
-                               list( $name, $param ) = $this->parseMatch( $m );
-                               $found[$name] = $param;
+                       $matches = array();
+                       $matched = preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
+                       if ( $matched === false ) {
+                               throw new Exception( __METHOD__ . ': preg_match_all returned false with error code '
+                                       . preg_last_error() );
+                       }
+                       if ( $matched ) {
+                               foreach ( $matches as $m ) {
+                                       list( $name, $param ) = $this->parseMatch( $m );
+                                       $found[$name] = $param;
+                               }
+                       }
+                       $replaced = preg_replace( $regex, '', $text );
+                       if ( $replaced !== null ) {
+                               $text = $replaced;
+                       } else {
+                               throw new Exception( __METHOD__ . ': preg_replace returned null with error code '
+                                       . preg_last_error() );
                        }
-                       $text = preg_replace( $regex, '', $text );
                }
                return $found;
        }