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 Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/ 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); Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/#findComment-117222 Share on other sites More sharing options...
AbydosGater Posted October 31, 2006 Author Share Posted October 31, 2006 Hey, That works great!Thanks Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/#findComment-117223 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 Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/#findComment-117273 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() Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/#findComment-117278 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) Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/#findComment-117279 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. Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/#findComment-117283 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 Link to comment https://forums.phpfreaks.com/topic/25685-sarching-a-string-not-strstr/#findComment-117290 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.