Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] static w php4
Forum PHP.pl > Forum > Przedszkole
Qbexus
Witam
Korzystam z takiej klasy
  1. <?php
  2. $word = 'pozycjonowanie';
  3.  
  4. class cTypoGenerator
  5. {
  6.  
  7. // array of keys near character on a QWERTY keyboard
  8. // only valid characters in a domain name
  9. static $keyboard = array(
  10. // top row
  11. '1' => array( '2', 'q' ),
  12. '2' => array( '1', 'q', 'w', '3' ),
  13. '3' => array( '2', 'w', 'e', '4' ),
  14. '4' => array( '3', 'e', 'r', '5' ),
  15. '5' => array( '4', 'r', 't', '6' ),
  16. '6' => array( '5', 't', 'y', '7' ),
  17. '7' => array( '6', 'y', 'u', '8' ),
  18. '8' => array( '7', 'u', 'i', '9' ),
  19. '9' => array( '8', 'i', 'o', '0' ),
  20. '0' => array( '9', 'o', 'p', '-' ),
  21. '-' => array( '0', 'p' ),
  22. // 2nd from top
  23. 'q' => array( '1', '2', 'w', 'a' ),
  24. 'w' => array( 'q', 'a', 's', 'e', '3', '2' ),
  25. 'e' => array( 'w', 's', 'd', 'r', '4', '3' ),
  26. 'r' => array( 'e', 'd', 'f', 't', '5', '4' ),
  27. 't' => array( 'r', 'f', 'g', 'y', '6', '5' ),
  28. 'y' => array( 't', 'g', 'h', 'u', '7', '6' ),
  29. 'u' => array( 'y', 'h', 'j', 'i', '8', '7' ),
  30. 'i' => array( 'u', 'j', 'k', 'o', '9', '8' ),
  31. 'o' => array( 'i', 'k', 'l', 'p', '0', '9' ),
  32. 'p' => array( 'o', 'l', '-', '0' ),
  33. // home row
  34. 'a' => array( 'z', 's' , 'w', 'q' ),
  35. 's' => array( 'a', 'z', 'x', 'd', 'e', 'w' ),
  36. 'd' => array( 's', 'x', 'c', 'f', 'r', 'e' ),
  37. 'f' => array( 'd', 'c', 'v', 'g', 't', 'r' ),
  38. 'g' => array( 'f', 'v', 'b', 'h', 'y', 't' ),
  39. 'h' => array( 'g', 'b', 'n', 'j', 'u', 'y' ),
  40. 'j' => array( 'h', 'n', 'm', 'k', 'i', 'u' ),
  41. 'k' => array( 'j', 'm', 'l', 'o', 'i' ),
  42. 'l' => array( 'k', 'p', 'o' ),
  43. // bottom row
  44. 'z' => array( 'x', 's', 'a' ),
  45. 'x' => array( 'z', 'c', 'd', 's' ),
  46. 'c' => array( 'x', 'v', 'f', 'd' ),
  47. 'v' => array( 'c', 'b', 'g', 'f' ),
  48. 'b' => array( 'v', 'n', 'h', 'g' ),
  49. 'n' => array( 'b', 'm', 'j', 'h' ),
  50. 'm' => array( 'n', 'k', 'j' )
  51. );
  52.  
  53.  
  54. // accepts a string
  55. // returns array of likely single "wrong key" typos
  56. // arrays contain only characters that are valid domain names
  57.  
  58. function getWrongKeyTypos( $word )
  59. {
  60. $word = strtolower( $word );
  61. $typos = array();
  62. $length = strlen( $word );
  63. // check each character
  64. for( $i = 0; $i < $length; $i++ )
  65. {
  66. // if character has replacements then create all replacements
  67. if( cTypoGenerator::$keyboard[$word{$i}] )
  68. {
  69. // temp word for manipulating
  70. $tempWord = $word;
  71. foreach( cTypoGenerator::$keyboard[$word{$i}] as $char )
  72. {
  73. $tempWord{$i} = $char;
  74. array_push( $typos, $tempWord );
  75. }
  76. }
  77. }
  78.  
  79. return $typos;
  80. }
  81.  
  82.  
  83.  
  84. // accepts a string
  85. // returns array of likely single missed character typos
  86. // arrays contain only characters that are valid domain names
  87. function getMissedCharTypos( $word )
  88. {
  89. $word = strtolower( $word );
  90. $typos = array();
  91. $length = strlen( $word );
  92. // check each character
  93. for( $i = 0; $i < $length; $i++ )
  94. {
  95. $tempWord = '';
  96. if( $i == 0 )
  97. {
  98. // at first character
  99. $tempWord = substr( $word, ( $i + 1 ) );
  100.  
  101. } else if ( ( $i + 1 ) == $length ) {
  102. // at last character
  103. $tempWord = substr( $word, 0, $i );
  104.  
  105. } else {
  106. // in between
  107. $tempWord = substr( $word, 0, $i );
  108. $tempWord .= substr( $word, ( $i + 1 ));
  109.  
  110. }
  111. array_push( $typos, $tempWord );
  112. }
  113.  
  114. return $typos;
  115. }
  116.  
  117.  
  118. // accepts a string
  119. // returns array of likely transposed character typos
  120. // arrays contain only characters that are valid domain names
  121. function getTransposedCharTypos( $word )
  122. {
  123. $word = strtolower( $word );
  124. $typos = array();
  125. $length = strlen( $word );
  126. // check each character
  127. for( $i = 0; $i < $length; $i++ )
  128. {
  129. if( ( $i + 1 ) == $length )
  130. {
  131. // could have simplified the test by throwing it in the for loop but I didn't to keep it readable
  132. // at the end no transposition
  133. } else {
  134. $tempWord = $word;
  135. $tempChar = $tempWord{$i};
  136. $tempWord{$i} = $tempWord{( $i + 1 )};
  137. $tempWord{( $i + 1 )} = $tempChar;
  138. array_push( $typos, $tempWord );
  139. }
  140. }
  141.  
  142. return $typos;
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149. // accepts a string
  150. // returns array of likely double entered character typos
  151. // arrays contain only characters that are valid domain names
  152. function getDoubleCharTypos( $word )
  153. {
  154. $word = strtolower( $word );
  155. $typos = array();
  156. $length = strlen( $word );
  157. // check each character
  158. for( $i = 0; $i < $length; $i++ )
  159. {
  160. // get first part of word
  161. $tempWord = substr( $word, 0, ($i+1) );
  162. // add a character
  163. $tempWord .= $word{$i};
  164. // add last part of strin if there is any 
  165. if( $i == ( $length - 1 ))
  166. {
  167. // do nothing we are at the end
  168. } else {
  169. // add the end part of the string
  170. $tempWord .= substr( $word, ($i+1));
  171. }
  172. array_push( $typos, $tempWord );
  173. }
  174. return $typos;
  175. }
  176. ?>


proszę używać poprawnego bbcode.
poprawiam
revyag


niestety obecnie mam serwer z php4 i juz mi to nie chodzi bo static jest tylko w php5 (chyba)

Czy da sie to jakoś przerobić żeby poszlo w php4?
bo potrzebuje to do strony ze slownikiem gdzie generowane sa potencjalne bledy w wyrazach.
erix
static dla php4 istnieje. Tylko nie możesz określać wartości zmiennej "w powietrzu"; musisz określić ją np. w konstruktorze:

  1. <?php
  2. class klasa{
  3.  
  4. var $zmienna;
  5.  
  6. function klasa(){
  7. $this->zmienna = 'wartosc';
  8. }
  9.  
  10. }
  11. ?>
Qbexus
Jak zwykle erix niezawodny dzieki.
A jak by to wygladalo w tym konkretnym przypadku? Wiem wiem chyba za wiele wymagam ale mam malo czasu na ten projekt a 1000 spraw w proszku smile.gif
erix
Cytat
Jak zwykle erix niezawodny dzieki.

Taak? smile.gif

A wracając:
  1. <?php
  2. static $keyboard = array(
  3. ?>

Utwórz w klasie var $keyboard (zgodnie z moim przykładem) i do funkcji konstruktora wrzuć całą tą tablicę, ale zmień pierwszą linijkę na:

  1. <?php
  2. $this->keyboard = array(
  3. ?>
Qbexus
Niestety jak mam tak
Kod
class cTypoGenerator
{
    var $keyboard;

$this->keyboard = array(
// top row
'1' => array( '2', 'q' ),
'2' => array( '1', 'q', 'w', '3' ),
'3' => array( '2', 'w', 'e', '4' ),
'4' => array( '3', 'e', 'r', '5' ),
'5' => array( '4', 'r', 't', '6' ),

to mi wyskakuje blad
Parse error: parse error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\Program Files\xampp\htdocs\xampp\Qba\misspell\misspell_4.php on line 8

a linia 8 to
Kod
$this->keyboard = array(

Ja chyba ciemny jestem bo mi to nie dziala.
erix
Bo to w funkcje trzeba wcisnąć!

  1. <?php
  2. class cTypoGenerator
  3. {
  4. var $keyboard;
  5.  
  6. function cTypoGenerator(){
  7. $this->keyboard = array(
  8. ?>


Mam nadzieję, że z resztą sobie poradzisz...
Qbexus
hehe dzieki tylko teraz trzeba chyba jakos odwolanie przerobic bo to
Kod
$typos = cTypoGenerator::getWrongKeyTypos( $word );

juz nie działa sad.gif
sorry za trucie ale na klasach sie jeszcze srednio znam.
erix
Utwórz najpierw instancję klasy. Jeśli bardzo Ci zależy na Twoim wywołaniu, to w każdej funkcji klasy wklep odwołanie do konstruktora.
Qbexus
Nie no wcale mi nie zalezy tylko nie wiem jak powinno wygladac nowe odwolanie ? bo teraz sa funkcje w funkcjach i jeszcze w klasie blinksmiley.gif
erix
  1. <?php
  2. $klasa = new nazwaKlasy;
  3. $klasa->funkcja;
  4. ?>

MANUAL, MANUAL i JESZCZE RAZ MANUAL!!!
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.