legohead6 Posted June 22, 2006 Share Posted June 22, 2006 ok..i am trying to figure out a way to replace swears from my forum into **** what happens is i insert into my database the word and replacement(works fine) and then im trying to use str_replace() to do the rest but its only replacing the last word like that(if there is more then one!, with one it works fine!) I have made a test sentance but i took out the words to post it here and replaced with (swear)heres the code!$[code]lang = array();$query = "SELECT * FROM words";$result = mysql_query($query);while($row = mysql_fetch_row($result)){$lang[$row[1]] = "$row[2]";}foreach($lang as $id => $value){$par2 = str_replace("$id","$value","What the (swear1) (swear2)");}echo "$par2";[/code]Please help! Link to comment https://forums.phpfreaks.com/topic/12615-swear-system/ Share on other sites More sharing options...
DieSucker Posted June 22, 2006 Share Posted June 22, 2006 [!--quoteo(post=386735:date=Jun 21 2006, 11:58 PM:name=legohead6)--][div class=\'quotetop\']QUOTE(legohead6 @ Jun 21 2006, 11:58 PM) [snapback]386735[/snapback][/div][div class=\'quotemain\'][!--quotec--]ok..i am trying to figure out a way to replace swears from my forum into **** what happens is i insert into my database the word and replacement(works fine) and then im trying to use str_replace() to do the rest but its only replacing the last word like that(if there is more then one!, with one it works fine!) I have made a test sentance but i took out the words to post it here and replaced with (swear)heres the code!$[code]lang = array();$query = "SELECT * FROM words";$result = mysql_query($query);while($row = mysql_fetch_row($result)){$lang[$row[1]] = "$row[2]";}foreach($lang as $id => $value){$par2 = str_replace("$id","$value","What the (swear1) (swear2)");}echo "$par2";[/code]Please help![/quote]maybe try preg_replace[a href=\"http://us2.php.net/manual/en/function.preg-replace.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.preg-replace.php[/a] Link to comment https://forums.phpfreaks.com/topic/12615-swear-system/#findComment-48371 Share on other sites More sharing options...
legohead6 Posted June 22, 2006 Author Share Posted June 22, 2006 [!--quoteo(post=386736:date=Jun 22 2006, 12:01 AM:name=DieSucker)--][div class=\'quotetop\']QUOTE(DieSucker @ Jun 22 2006, 12:01 AM) [snapback]386736[/snapback][/div][div class=\'quotemain\'][!--quotec--]maybe try preg_replace[a href=\"http://us2.php.net/manual/en/function.preg-replace.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.preg-replace.php[/a][/quote]k heres the code[code](line: 10)$connection = mysql_connect($host,$user,$pass) or die ("Unable toconnect!");$sent="What the (swear1) (swear2)";mysql_select_db($db) or die ("Unable to select database!");$lang = array();$query = "SELECT * FROM words";$result = mysql_query($query);$i=0;while($row = mysql_fetch_row($result)){$word[$i] = "$row[1]";$replace[$i] = "$row[2]";$i++;}$par = preg_replace("$word","$replace","$sent");echo "$par2";(line: 25)echo "$sent";[/code]it comes up withWarning: Delimiter must not be alphanumeric or backslash in /var/www/html/store/swear starer/test.php on line 23What the (swear1) (swear2) Link to comment https://forums.phpfreaks.com/topic/12615-swear-system/#findComment-48373 Share on other sites More sharing options...
shortj75 Posted June 22, 2006 Share Posted June 22, 2006 there is a very simple salution here is what i do create a page and name it word .php and heres the code you will need[code]word.php<?phpfunction Smiley($texttoreplace){ $smilies=array( //example: "swearword"=>"what you would like to display on your page"; "fuc*"=>"*$#@", "shi@"=>"&%$@","as*"=>"*%$", //and so on just add the words you want and change fuc* shi@ and as* to the real words ); $texttoreplace=str_replace(array_keys($smilies), array_values($smilies), $texttoreplace); return $texttoreplace;}?>[/code]and on your output page test.php place the folowing[code]test.php<?include 'word.php';your code hereecho smiley($par2);?>[/code]i use this script to do the opposite for dirty word on this page [a href=\"http://tfws.dynu.com/joeleinegray/\" target=\"_blank\"]CLICK HERE[/a] when you type in a swear word it say a sentence back to you Link to comment https://forums.phpfreaks.com/topic/12615-swear-system/#findComment-48386 Share on other sites More sharing options...
legohead6 Posted June 22, 2006 Author Share Posted June 22, 2006 [!--quoteo(post=386751:date=Jun 22 2006, 01:46 AM:name=shortj75)--][div class=\'quotetop\']QUOTE(shortj75 @ Jun 22 2006, 01:46 AM) [snapback]386751[/snapback][/div][div class=\'quotemain\'][!--quotec--]there is a very simple salution here is what i do create a page and name it word .php and heres the code you will need[code]word.php<?phpfunction Smiley($texttoreplace){ $smilies=array( //example: "swearword"=>"what you would like to display on your page"; "fuc*"=>"*$#@", "shi@"=>"&%$@","as*"=>"*%$", //and so on just add the words you want and change fuc* shi@ and as* to the real words ); $texttoreplace=str_replace(array_keys($smilies), array_values($smilies), $texttoreplace); return $texttoreplace;}?>[/code]and on your output page test.php place the folowing[code]test.php<?include 'word.php';your code hereecho smiley($par2);?>[/code]i use this script to do the opposite for dirty word on this page [a href=\"http://tfws.dynu.com/joeleinegray/\" target=\"_blank\"]CLICK HERE[/a] when you type in a swear word it say a sentence back to you[/quote]Thanks i your code and merged it with mine and came out with this that works perfectly[code]$connection = mysql_connect($host,$user,$pass) or die ("Unable toconnect!");$sent=$_POST['message'];mysql_select_db($db) or die ("Unable to select database!");$lang = array();$query = "SELECT * FROM words";$result = mysql_query($query);while($row = mysql_fetch_row($result)){$lang[$row[1]] = "$row[2]";}$par = str_replace(array_keys($lang), array_values($lang),"$sent");echo "$par<br><br>";}echo "<form method=POST>Message:<textarea name=message></textarea><br><input type=submit name=submit value=submit></form>";?>[/code]heres a page you can try it on ... only b**** a** and f*** work currently i will add more soon![a href=\"http://shop.mattswebpage.com/swear%20starer/test.php\" target=\"_blank\"]http://shop.mattswebpage.com/swear%20starer/test.php[/a] Link to comment https://forums.phpfreaks.com/topic/12615-swear-system/#findComment-48525 Share on other sites More sharing options...
Koobi Posted June 22, 2006 Share Posted June 22, 2006 str_replace() accepts arrays in the search and replace parameters:[code]$search = $replace = array();$connection = mysql_connect($host,$user,$pass) or die ("Unable toconnect!");$sent=$_POST['message'];mysql_select_db($db) or die ("Unable to select database!");$lang = array();$query = "SELECT * FROM words";$result = mysql_query($query);while($row = mysql_fetch_row($result)){$search[] = $row[1];$replace[] = $row[2];}$par = str_replace($search, $replace, $sent);echo "$par<br><br>";}echo "<form method=POST>Message:<textarea name=message></textarea><br><input type=submit name=submit value=submit></form>";[/code] Link to comment https://forums.phpfreaks.com/topic/12615-swear-system/#findComment-48529 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.