Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [class]stringProcessing, zbior funkcji do zmiany stringa
kriqs
post
Post #1





Grupa: Zarejestrowani
Postów: 148
Pomógł: 0
Dołączył: 31.05.2006

Ostrzeżenie: (0%)
-----


taka fajna klassa byla mi potrzebna do bloga aby tworzyc tagi (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

  1. <?php
  2. /**
  3. * Classa do obslugi stringa usuwanie niedozwolonych znakow i zmiany
  4. *
  5. * @author Kriqs <mr.kriqs@gmail.com>
  6. * @version 0.2
  7. * @name stringProcessing
  8. */
  9.  
  10. class stringProcessing
  11. {
  12. /**
  13.   * Usuwa ze stringu wszystkie znaki ktore nie sa w paternie
  14.   * @param string $string Tekst do przerobienia, string $pattern wzor z ktorego ko
    zysta funkcja
  15.   * @return string
  16.   */
  17. public static function onlyAcceptChar( $string , $pattern )
  18. {
  19. preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
  20. $text ='';
  21. foreach( $matches[0] as $char )
  22. {
  23. $text.=$char[0];
  24. }
  25. return $text;
  26. } 
  27.  
  28. /**
  29.   * Zmienia znaki 
  30.   * @param string $string Tekst do przerobienia, 
  31.   * @param mixed $from - $from = array( 'cos'=>'na' ) i $on = null, - $from = array( 'cos', 'cos2') na $on - $from = 'cos' na $on
  32.   * @return string
  33.   */
  34. public static function changeChar( $string , $from , $on = null )
  35. {
  36. if (is_null($on)){
  37. $on = array_values($from);
  38. $from = array_keys($from);
  39. }
  40. return str_replace( $from , $on , $string );
  41. }
  42. }
  43. ?>


Przyklady:
  1. <?php
  2. $string = 'Cze tu jest test [100] - 999 ';
  3. $pattern = '/([a-zA-Z0-9_ ])/';
  4.  
  5. echo stringProcessing::onlyAcceptChar($string , $pattern );
  6. echo '<br>';
  7. echo stringProcessing::changeChar( $string , array( ' ', 'x' ) , '_');
  8. echo '<br>';
  9. echo stringProcessing::changeChar( $string , array( ' '=>'_' , 'cze'=>'hello' ));
  10. ?>


nie wiem czy sie przyda (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) i pewnie da sie latwiej (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) ale luz.


dziekuje

Ten post edytował kriqs 5.04.2007, 15:30:47
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 13:51