YourNameHere Posted September 19, 2009 Share Posted September 19, 2009 So, I dint know much about reg ex and want to learn but the manual is hard to understand. This is what I want... I have a chatroom and I want to add the functionality of IRCs "/me" command. so I thought preg_match would be best for this. sample code: $message = "/me is here!"; $pattern = "???"; if (preg_match($pattern, $message)) { //Preg_Match insert into DB } else { //normal insert into DB } I need to see if "/me " are the first 4 chars of the string Then I need to delete "/me " from the string and then insert the new string into the DB Otherwise Insert the string as it is into the DB. I have the insert functions working but I just need to do an if statement to see if /me is at the beginning of the string. I think that is all the info I need to post for this forum, if not, let me know. Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/ Share on other sites More sharing options...
Garethp Posted September 19, 2009 Share Posted September 19, 2009 if(preg_match("~^/me (.*)$~s", $message, $Matches); { //The message is in $Matches[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921342 Share on other sites More sharing options...
nrg_alpha Posted September 19, 2009 Share Posted September 19, 2009 Alternatively, Example: $message = "/me is here!"; $message = (strpos($message, '/me ') === 0)? substr($message, 4): $message; Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921352 Share on other sites More sharing options...
Garethp Posted September 19, 2009 Share Posted September 19, 2009 How does that second line work? Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921354 Share on other sites More sharing options...
nrg_alpha Posted September 19, 2009 Share Posted September 19, 2009 What that second line does is use strpos to check to see if the character sequence '/me ' is there and starts at position 0 in the string (therefore, the beginning of the string). If so, then $message will be equal to itself starting at position 4 (which comes right after the space after /me) by use of substr, otherwise, it is simply equal to itself (nothing has changed). Hopefully I explained that adequately. EDIT - And of course, this is all done in a ternary operator fashion...as opposed to the typical if(...){ ... } else { ... } Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921360 Share on other sites More sharing options...
YourNameHere Posted September 19, 2009 Author Share Posted September 19, 2009 Alternatively, Example: $message = "/me is here!"; $message = (strpos($message, '/me ') === 0)? substr($message, 4): $message; How would I put that into an IF statement? I basically need to insert into the database for the chat column "me" = yes when /me is used Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921366 Share on other sites More sharing options...
MadTechie Posted September 19, 2009 Share Posted September 19, 2009 How would I put that into an IF statement? I basically need to insert into the database for the chat column "me" = yes when /me is used You don't need to put it into an if, as it's a "ternary" (aka shorthand if), Your could do this $message = "/me is here!"; if (strpos($message, '/me ') === 0) { $message = substr($message, 4); //remove the "/me " //Preg_Match insert into DB }else{ //normal insert into DB } BUT if the insert into database is going to be the same, then this $message = "/me is here!"; $message = (strpos($message, '/me ') === 0)? substr($message, 4): $message; is the same as $message = "/me is here!"; if (strpos($message, '/me ') === 0) { $message = substr($message, 4); //remove the "/me " } //normal insert into DB Make sense ? Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921367 Share on other sites More sharing options...
YourNameHere Posted September 19, 2009 Author Share Posted September 19, 2009 Yes it makes sense but it seems to break my code. $charname = $_GET['char']; $msg = $_GET['msg']; $time = time(); $message = $_GET['msg']; if (strpos($message, '/me ') === 0) { $message = substr($message, 4); //remove the "/me " $insert = "INSERT INTO chat (time, username, msg, me) values ('$time', '$charname', '$message', 'y')"; mysql_query($insert); } else { $insert = "INSERT INTO chat (time, username, msg) values ('$time', '$charname', '$msg')"; mysql_query($insert); } ?> I cant put actual data in the vars to make it easier for you. I just isnt inserting into to DB Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921375 Share on other sites More sharing options...
MadTechie Posted September 19, 2009 Share Posted September 19, 2009 If its not working i would guess you have a query problem try this $charname = $_GET['char']; $msg = $_GET['msg']; $time = time(); $message = $_GET['msg']; if (strpos($message, '/me ') === 0) { $message = substr($message, 4); //remove the "/me " $insert = "INSERT INTO chat (time, username, msg, me) values ('$time', '$charname', '$message', 'y')"; mysql_query($insert) or die(mysql_error()); //updated }else{ $insert = "INSERT INTO chat (time, username, msg) values ('$time', '$charname', '$msg')"; mysql_query($insert); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921381 Share on other sites More sharing options...
YourNameHere Posted September 19, 2009 Author Share Posted September 19, 2009 Awesome it works now! I guess that is one of the problems you run into with ajax. There was an error because I forgot to add the last "}" so i wasnt seeing the error, thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/174826-solved-simple-preg_match/#findComment-921385 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.