[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/ -> stringutils.class.php (source)

   1  <?php
   2  
   3      
   4  
   5      /**

   6       * \ingroup Data

   7       *

   8       * Basic methods for formatting or doing nasty things to strings :-)

   9       */
  10      class StringUtils  
  11      {
  12  
  13          /** 

  14           * Alias for htmlspecialchars

  15           * @static

  16           */
  17          function htmlTranslate( $string )
  18          {
  19              return htmlspecialchars( $string );
  20          }
  21  
  22          /**

  23           * cuts a string at a given character

  24           *

  25           * @param string

  26           * @param n

  27           * @return The reduced string

  28           * @static

  29           */
  30          function cutString( $string, $n )
  31          {
  32              return substr( $string, 0, $n );
  33          }
  34  
  35          /**

  36           * Returns an array with all the links in a string.

  37           *

  38           * @param string The string

  39           * @return An array with the links in the string.

  40           * @static

  41           */
  42          function getLinks( $string )
  43          {
  44          $regexp = "|<a[^>]+href=[\"']{0,1}([^'\"]+?)[\"']{0,1}[^>]*>(.+)</a>|iU";
  45              $result = Array();
  46  
  47              if( preg_match_all( $regexp, $string, $out, PREG_PATTERN_ORDER )) {
  48                  foreach( $out[1] as $link ) {
  49                       array_push( $result, $link );
  50                  }
  51              }
  52  
  53              return $result;
  54          }
  55          
  56          /**

  57           * Returns a size formatted and with its unit: "bytes", "KB", "MB" or "GB"

  58           *

  59           * @param size The amount

  60           * @return A string with the formatted size.

  61           * @static

  62           */
  63  		function formatSize( $size )
  64          {
  65              if ($size < pow(2,10)) return $size." bytes";
  66              if ($size >= pow(2,10) && $size < pow(2,20)) return round($size / pow(2,10), 0)." KB";
  67              if ($size >= pow(2,20) && $size < pow(2,30)) return round($size /pow(2,20), 1)." MB";
  68              if ($size > pow(2,30)) return round($size / pow(2,30), 2)." GB";
  69          }
  70  
  71          /**

  72           * Returns a string in a readable and url-compliant format.

  73           *

  74           * @param string The string

  75           * @return A string ready to use in urls.

  76           * @static

  77           * @see TextFilter::urlize()

  78           */
  79          function text2url( $string )
  80          {
  81              // remove unnecessary spaces and make everything lower case

  82              $string = preg_replace( "/ +/", " ", strtolower($string) );
  83  
  84              // special rule for dashes, I think it looks nicer :-p

  85              $string = str_replace(' - ', '-', $string);
  86  
  87              // removing a set of reserved characters (rfc2396: ; / ? : @ & = + $ ,)

  88              $string = str_replace(array(';','/','?',':','@','&','=','+','$',','), '', $string);
  89  
  90              // replace some characters to similar ones (more readable uris)

  91              $search  = array(' ', 'Š', 'š', 'Ÿ','‘','•','Ž','','ˆ','',);
  92              $replace = array('_','ae','oe','ue','e','i','e','e','a','c');
  93              $string = str_replace($search, $replace, $string);
  94  
  95              // remove everything we didn't so far...

  96              $string = preg_replace("/[^a-z0-9_-]/", "", $string);
  97              
  98              // urlencode everything, in case we missed something ;-)

  99              return urlencode($string);
 100          }
 101          
 102          /**

 103           * extremely lame function. 

 104           *

 105           * @param count how many times we'd like to repeat the character

 106           * @param char The character (or string) we'd like to repeat

 107           * @return The resulting string

 108           * @static

 109           */
 110   		function pad( $count, $char = " ")
 111          {
 112              $i=0;
 113              $result = "";
 114              while( $i < $count ) {
 115                  $result .= $char;
 116                  $i++;
 117              }
 118              
 119              return $result;
 120          }        
 121      }
 122  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics