ILYAS415 Posted February 19, 2008 Share Posted February 19, 2008 Okay i'm making a chat room thing and i would like to know how check if the user has entered a certain word into his her string. Basically im trying to make a /me commad (the typical action command). I have already tried using this.... strpos("/me", $string, 1); however that means that the user can enter in.... wow the /me command is wicked and if will be seen as an action command because it got /me inside it. I only want the script to look for /me if it is in the beggining of a sentance. Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/ Share on other sites More sharing options...
phpSensei Posted February 19, 2008 Share Posted February 19, 2008 preg_match('{word}','text'); maybe? Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471126 Share on other sites More sharing options...
cooldude832 Posted February 19, 2008 Share Posted February 19, 2008 good plan is BB tags you can use them to apply basic styling, and you can probably get your desired affect from it. Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471127 Share on other sites More sharing options...
Barand Posted February 19, 2008 Share Posted February 19, 2008 <?php if (strpos($string, "/me") === 0) { // its at the beginning } ?> Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471137 Share on other sites More sharing options...
ILYAS415 Posted February 19, 2008 Author Share Posted February 19, 2008 Barand your code didnt appear to work for me. phpSensai im not sure how exactly to use the function your gave me. Can you please give me a snippet based on the example i gave? hmm im considering bbcodes as my last option. Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471146 Share on other sites More sharing options...
Barand Posted February 19, 2008 Share Posted February 19, 2008 <?php $string = '/me foo bar'; if (strpos($string, "/me") === 0) { echo 'its at the beginning'; } else { echo 'Oh no its not'; } ?> Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471153 Share on other sites More sharing options...
ILYAS415 Posted February 19, 2008 Author Share Posted February 19, 2008 Nope not working... :S heres my code im using... if (strpos($msg, "/me") === "0"){ $msg= str_replace("/me", "", $msg); $out="$chattext ".ucfirst($fetch->username)." <font color=white>$msg</font><br>"; }else{ $out = $chattext . $n . " - " . $msg . "<br>"; $out = str_replace("\'", "'", $out); $out = str_replace("\\\"", "\"", $out); } $out= chat($out); Thanks again Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471156 Share on other sites More sharing options...
Barand Posted February 19, 2008 Share Posted February 19, 2008 Lose the quotes round the 0 if (strpos($msg, "/me") === "0") Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471165 Share on other sites More sharing options...
ILYAS415 Posted February 19, 2008 Author Share Posted February 19, 2008 Nope still not working :S comes out as.... Ilyas415 - /me message here instead of ilyas415 message here Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471171 Share on other sites More sharing options...
Barand Posted February 19, 2008 Share Posted February 19, 2008 Don't have $fetch object so I changed $fetch->username to $username. Also don't have you chat() function, but <?php $username = 'xyz'; $msg = '/me foo bar'; $chattext = 'abc'; if (strpos($msg, "/me") === 0){ $msg= str_replace("/me", "", $msg); $out="$chattext ".ucfirst($username)." <font color=white>$msg</font><br>"; }else{ $out = $chattext . $n . " - " . $msg . "<br>"; $out = str_replace("\'", "'", $out); $out = str_replace("\\\"", "\"", $out); } echo $out; // --> abc Xyz <font color=white> foo bar</font><br> ?> Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471184 Share on other sites More sharing options...
BrandonK Posted February 20, 2008 Share Posted February 20, 2008 what if the person is using /me in more than one spot -- perhaps explaining how to use /me: [/me things that you do an emote you simply say "/me emote"] Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471216 Share on other sites More sharing options...
cooldude832 Posted February 20, 2008 Share Posted February 20, 2008 thus why I said use bbCode Link to comment https://forums.phpfreaks.com/topic/91988-how-to-look-for-a-word-in-a-string/#findComment-471297 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.