AbydosGater Posted October 31, 2006 Share Posted October 31, 2006 Hey, Ive been working on a chat script for my website, its going great, but my members have been asking me to add a similies set to chat!And i said yeah it would be easy..All i have to do is check if the string contains one of the keywords, (ie : P) and then if it does use str_replace?, as far as i know thats correct!But how would i search if my string contains the keyword?I was going to use strstr, but on reading the manual i relised that this only..."Returns part of haystack string from the first occurrence of needle to the end of haystack. "What should i use?Thanks Abydos Quote Link to comment Share on other sites More sharing options...
Cep Posted October 31, 2006 Share Posted October 31, 2006 Why not use the str replace function?for example,$string = "im great :) oh yes";$newstring = str_replace(":)", "<img src='mypic.gif' id=':)' alt='smilie' />", $string); Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted October 31, 2006 Author Share Posted October 31, 2006 Hey, That works great!Thanks Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted October 31, 2006 Author Share Posted October 31, 2006 Hi, back again! i tryed this, and no success!I made a function...[code]function modifyPost($message){$message = $message;$message = str_replace("\n", "<br />", $message);$message = str_replace("fuck", "----", $message);return $message ;};[/code]and it is run just before the post is submitted to the database...[code]if($message){modifyPost($message);mysql_query("INSERT INTO chat_messages (message_id, member_id, handle, ip, timestamp, message, type) VALUES (NULL , '0', '$handle', '$ip', CURRENT_TIMESTAMP , '$message', 'post') ") or die(mysql_error()); };[/code]It should replace the "f" word with ----, but it doesnt, it runs ok and the message is added to the database, but it doesnt add a <br> for every newline in the message and it doesnt censor the word!Why is it not working??Thanks abydos Quote Link to comment Share on other sites More sharing options...
bljepp69 Posted October 31, 2006 Share Posted October 31, 2006 Your function is returning a value so you need to have something to receive that value. Change this:modifyPost($message);to$cleanMessage = modifyPost($message);and then use $cleanMessage in your db query.Also, check out nl2br() Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted October 31, 2006 Share Posted October 31, 2006 You would be better off using preg_replace and generating an arary keywords (like ': P') and and an array of corresponding smileys '<img src=....'that way you can do all replacements at once with just one line fo code (and the two arrays of course) Quote Link to comment Share on other sites More sharing options...
Orio Posted October 31, 2006 Share Posted October 31, 2006 str_replace can also use arrays, and is faster than preg_match.Orio. Quote Link to comment Share on other sites More sharing options...
AbydosGater Posted October 31, 2006 Author Share Posted October 31, 2006 Yay! I have it up and running good now with str_replace, so im going to leave it at that!But thank you all so much! been a great helpThanks Abydos Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.