Changeset 434


Ignore:
Timestamp:
Nov 8, 2013 3:56:09 AM (10 years ago)
Author:
anonymous
Message:

Improved SpellCheck? preg pattern

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.1dev/lib/SpellCheck.inc.php

    r431 r434  
    186186   
    187187    /**
    188      * Returns an array of suggested words for each mispelled word in the given text.
     188     * Returns an array of suggested words for each misspelled word in the given text.
    189189     * The first word of the returned array is the (possibly) misspelled word.
    190190     *
     
    201201        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
    202202        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
    203         $words = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(\p{P}+$))/", $string);
     203        $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    204204        if (is_array($words) && !empty($words)) {
    205205            // Remove non-word elements.
     
    237237        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
    238238        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
    239         $words = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(\p{P}+$))/", $string);
     239        $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    240240        if (is_array($words) && !empty($words)) {
    241241            // Remove non-word elements.
     
    270270        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
    271271        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
    272         $words = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(\p{P}+$))/", $string);
     272        $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    273273        $cnt = 0;
    274274        if (is_array($words) && !empty($words)) {
     
    279279                    $footnote = $show_footnote ? '<sup style="color:#999;">' . ++$cnt . '</sup>' : '';
    280280                    $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote;
    281                 }
    282             }
    283         }
    284         return join(' ', $words);
    285     }
    286    
    287     /**
    288      * Prints the HTML for correcting all mispellings found in the text of one $_FORM element.
     281                    $string = preg_replace("/\b$word\b/", $words[$i], $string);
     282                }
     283            }
     284        }
     285        return $string;
     286        // return join(' ', $words);
     287    }
     288   
     289    /**
     290     * Prints the HTML for correcting all misspellings found in the text of one $_FORM element.
    289291     *
    290292     * @access  public
     
    361363        // Split words on punctuation except apostrophes (this regex is used in several places in this class).
    362364        // http://stackoverflow.com/questions/790596/split-a-text-into-single-words
    363         $form_words = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(\p{P}+$))/", getFormData($form_name));
     365        $form_words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}—–-]+|(?:\p{P}+$))/", getFormData($form_name), -1, PREG_SPLIT_DELIM_CAPTURE);
    364366        $suggestions = getFormData('spelling_suggestions');
    365367        $corrections = getFormData('spelling_corrections');
     
    379381
    380382        if (is_array($form_words) && !empty($form_words)) {
    381             return join(' ', $form_words);
     383            return join('', $form_words);
    382384        } else {
    383385            return getFormData($form_name);
Note: See TracChangeset for help on using the changeset viewer.