Advertisement
Guest User

Untitled

a guest
Oct 21st, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. $bad = array("bad", "words", "here");
  2. $good = array("good", "words", "here");
  3.  
  4. function noswear($string)
  5. {
  6. if ($string)
  7. {
  8. $bad = array("bad", "words");
  9. $good = array("good", "words");
  10. $newstring = str_replace($bad, $good, $string);
  11. return $newstring;
  12. }
  13.  
  14. echo noswear("I see bad words coming!");
  15.  
  16. function badWordsFilter($inputWord) {
  17. $badWords = Array("bad","words","here");
  18. for($i=0;$i<count($badWords);$i++) {
  19. if($badWords[$i] == strtolower($inputWord))
  20. return true;
  21. }
  22. return false;
  23. }
  24.  
  25. if (badWordsFilter("bad")) {
  26. echo "Bad word was found";
  27. } else {
  28. echo "No bad words detected";
  29. }
  30.  
  31. function badWordsFilter($inputWord) {
  32. $badWords = Array("bad","words","here");
  33. if(in_array(strtolower($inputWord), $badWords) ) {
  34. return true;
  35. }
  36. return false;
  37. }
  38.  
  39. $wordsTransform = array(
  40. 'shit' => 'ship'
  41. );
  42.  
  43. $string = "Rolling In The Deep by Adeln
  44. n
  45. There's a fire starting in my heartn
  46. Reaching a fever pitch, and it's bringing me out the darkn
  47. Finally I can see you crystal clearn
  48. Go ahead and sell me out and I'll lay your shit bare";
  49.  
  50. $string = strtr($string, $wordsTransform);
  51.  
  52. //
  53. // Written by Patrick Rauchfuss
  54. class String
  55. {
  56. public static function stritr(&$string, $from, $to = NULL)
  57. {
  58. if(is_string($from))
  59. $string = preg_replace("/b{$from}b/i", $to, $string);
  60.  
  61. else if(is_array($from))
  62. {
  63. foreach ($from as $key => $val)
  64. self::stritr($string, $key, $val);
  65. }
  66. return preg_quote($string); // return and add a backslash to special characters
  67. }
  68. }
  69.  
  70. $wordsTransform = array(
  71. 'shit' => 'ship'
  72. );
  73.  
  74. String::stritr($string, $wordsTransform);
  75.  
  76. $input_string = 'This Could be interesting but should it be? Perhaps this 'would' work; or couldn't it?';
  77.  
  78. $bad_words = array('could', 'would', 'should');
  79. $good_words = array('might', 'will');
  80.  
  81. function replace_words($matches){
  82. global $good_words;
  83. return $matches[1].$good_words[rand(0, count($good_words)-1)].$matches[3];
  84. }
  85.  
  86. echo preg_replace_callback('/(^|b|s)('.implode('|', $bad_words).')(b|s|$)/i', 'replace_words', $input_string);
  87.  
  88. /(START OR WORD_BOUNDARY OR WHITE_SPACE)(BAD_WORD)(WORD_BOUNDARY OR WHITE_SPACE OR END)/i
  89.  
  90. global $good_words; <-- Makes the $good_words variable accessible from within the function
  91. $matches[1] <-- The word boundary before the matched word
  92. $matches[3] <-- The word boundary after the matched word
  93. $good_words[rand(0, count($good_words)-1] <-- Selects a random good word from $good_words
  94.  
  95. echo preg_replace_callback(
  96. '/(^|b|s)('.implode('|', $bad_words).')(b|s|$)/i',
  97. function ($matches) use ($good_words){
  98. return $matches[1].$good_words[rand(0, count($good_words)-1)].$matches[3];
  99. },
  100. $input_string
  101. );
  102.  
  103. function clean_string($input_string, $bad_words, $good_words){
  104. return preg_replace_callback(
  105. '/(^|b|s)('.implode('|', $bad_words).')(b|s|$)/i',
  106. function ($matches) use ($good_words){
  107. return $matches[1].$good_words[rand(0, count($good_words)-1)].$matches[3];
  108. },
  109. $input_string
  110. );
  111. }
  112.  
  113. echo clean_string($input_string, $bad_words, $good_words);
  114.  
  115. This will be interesting but might it be? Perhaps this 'will' work; or couldn't it?
  116. This might be interesting but might it be? Perhaps this 'might' work; or couldn't it?
  117. This might be interesting but will it be? Perhaps this 'will' work; or couldn't it?
  118.  
  119. foreach($bad_words as $key=>$word){
  120. $bad_words[$key] = preg_quote($word);
  121. }
  122.  
  123. b$h1tb <---Will not match
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement