special functions : corrects the punctuation

Discussion in 'PHP' started by French Directory, Feb 1, 2010.

  1. #1
    Hello,

    I wanted to share a special directory functions, it corrects the punctuation of a description.

    Here is what the correct punctuation:

    - Put a space after a period, a semicolon ... and one capitalize the word following point
    - Put a space after a comma
    - Puts a majucule to main content
    - Removes spaces at the beginning and end of string

    The + + +

    - Does not affect the 3 series or 2 points (...)
    - Does not affect smileys
    - Does not affect the number of signs ?!?!?
    - Does not affect the single and double odds "and"
    - Does not affect the HTML
    - Does not affect the URLs (because the correction is not running)
    - Does not include email addresses (because the correction is not running)
    - Does not affect Tel No: 01.04.01 ...
    - Does not affect the number 0.10 ...




    <?php
    function corrige_ponctuation($chaine) {
    
    /*****************************************************************************/
    /* initialization                                                       */
    /*****************************************************************************/
    
    
      $signes = array(".","!","?",",",":",";");
      $signes_connexe = array('"',")",">","'");
      $i=0;
      $taille_chaine = strlen( $chaine );
    
      $masque_url  = '#';
      $masque_url .= '([http|ftp|https]+://';
      $masque_url .= '[www\.]?';
      $masque_url .= '[\.\-a-zA-Z0-9]*';
      $masque_url .= '[a-zA-Z]{2,4}?';
      $masque_url .= '[\?/\=\&\-_.a-zA-Z0-9]+?';
     
    
      $masque_url .= '[\?/\=\&\-_.a-zA-Z0-9]+?';
    
    
      $masque_url .= '[\?/\=\&\-_.a-zA-Z0-9]+)';
      $masque_url .= '#i';
    
    
      $masque_email  = '#';
      $masque_email .= "[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*";
      $masque_email .= "@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})+";
      $masque_email .= '#';
    
    /*****************************************************************************/
    /*If 1 url or 1 email address is in the text, the correction stop */
    /*****************************************************************************/
    
    if (!preg_match_all($masque_url,$chaine,$match_1) and
        !preg_match_all($masque_email,$chaine,$match_2)
       )
    {
    /*****************************************************************************/
    /* beginning of the correction                                                  */
    /*****************************************************************************/
    
      while ( $i < $taille_chaine )
        {
        // met la premiere lettre du texte en majuscule
        if ($i==0) $chaine[$i] = strtoupper($chaine[$i]);
        // capture la chaine deja analysée
        $capture .= $chaine[$i];
    
    
    /*****************************************************************************/
    /* correct punctuation                                             */
    /*****************************************************************************/
    
         
          if( in_array($chaine[$i],$signes )
                   and $chaine[$i+1] != ' '
                   and !is_numeric( $chaine[$i+1] )
                   and !is_numeric( $chaine[$i-1] )
                   and ($chaine[$i+1] < $taille_chaine)
                   and ($chaine[$i+1] != '.')
                   and ($chaine[$i+1] != in_array($chaine[$i+1],$signes ))
                   and ($chaine[$i+1] != in_array($chaine[$i+1],$signes_connexe ))
                                           )
           {
            $capture_trans = $capture.' ';
            // insere le point + 1 espace
            $chaine = str_replace( $capture, $capture_trans, $chaine );
    
            // met la lettre en majuscule si on ne traite pas une virgule
            if($chaine[$i] != ',')
            $chaine[$i+2] = strtoupper($chaine[$i+2]);
           }
    
    /*****************************************************************************/
    /* puts a capital letter after a sign                                          */
    /*****************************************************************************/
         if (in_array($chaine[$i],$signes ) and ($chaine[$i+1] == ' ')
                                            and ($chaine[$i] != ',') )
          $chaine[$i+2] = strtoupper($chaine[$i+2]);
    
          $i++;
        }
        return trim($chaine);
       }
      }
    /*****************************************************************************/
    /* test                                                                      */
    /*****************************************************************************/
    
    // pour l'utiliser : 
    
       echo corrige_ponctuation($texte);
    
    
    ?>
    Code (markup):

     
    French Directory, Feb 1, 2010 IP