CodyBecker Posted January 4, 2010 Share Posted January 4, 2010 Hi all, (Im new to this so go easy on me ) I have a messaging system set up and i want one input field to contain the location to be sent and the message. So i basically want a text-box that will contain this information in this format.... [location], [text to send] so . . . . . if i were sending a message to ABC i would type into the text box . . . ABC, Hi how are you So im guessing i would use explode to find the receiver name (ABC) and the message text (Hi how are you) and post them. Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/ Share on other sites More sharing options...
laffin Posted January 4, 2010 Share Posted January 4, 2010 if u use explode, than ABC, Hey There, What's Up? Wont work. either use strpos of preg_match Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988000 Share on other sites More sharing options...
CodyBecker Posted January 4, 2010 Author Share Posted January 4, 2010 Any examples or code, like i said im a bit new. My whole website is created from bits of code of other websites. Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988001 Share on other sites More sharing options...
gizmola Posted January 4, 2010 Share Posted January 4, 2010 So the first thing I can do to help you is to teach you to fish -- on php.net. If you use php.net, and append the name of a function, you'll go to the online manual for that function. As was suggested: http://www.php.net/strpos -- should let you look for the location of the first comma. Your assumption is that before that is the user's name. Once you know that the substr function can get you the parts of the string. http://www.php.net/substr These will give you a lot of control. With that said, explode is pretty great function, and you can actually get around laffin's issue using the optional param that limits the number of pieces to 2. // $input should be sanitized version of the message from the $_POST. $msg = explode(',', $input); $user = trim($msg[0]); $text = trim($msg[1]); Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988004 Share on other sites More sharing options...
CodyBecker Posted January 4, 2010 Author Share Posted January 4, 2010 Will that allow for a blank space after the comma to start the message? ABC,[space][Message] Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988005 Share on other sites More sharing options...
oni-kun Posted January 4, 2010 Share Posted January 4, 2010 Does anyone read the manual anymore? explode remember parameters. //Here's the voodoo $msg = explode(',', $input, 2); $user = trim($msg[0]); $text = trim($msg[1]); EDIT: OP, It will allow anything before or after the first comma. trim will remove the space in case they write 'ABC, message after spaces'. Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988008 Share on other sites More sharing options...
gizmola Posted January 4, 2010 Share Posted January 4, 2010 Whooopsie .. I left out the optional parameter in my example. Should have been: $msg = explode(',', $input, 2); I used trim to get rid of start and ending whitespace around the parsed elements. It is going to parse the string on the first comma it finds and split that into 2 pieces that it sticks in the array. If you want to retain the whitespace you can, by not trimming, although I don't think that's a great idea. Regardless, explode doesn't care if there's whitespace, and it will retain that ... it simply breaks the string up on the first comma it finds. Last but not least, if you have a question about what something does, it's a good idea to write a little test script and try it out. Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988009 Share on other sites More sharing options...
oni-kun Posted January 4, 2010 Share Posted January 4, 2010 Beat you to it, Gizmola Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988010 Share on other sites More sharing options...
CodyBecker Posted January 4, 2010 Author Share Posted January 4, 2010 Im sorry guys i totally forgot, this text-box works as a command line so messages are initiated with a MSG command . . . . So in reality i want the user to type in the following into the text box... MSG ABC, Hi how are you Will this still work for this case? $msg = explode(',', $input, 1); $user = trim($msg[0]); $text = trim($msg[1]); Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988011 Share on other sites More sharing options...
gizmola Posted January 4, 2010 Share Posted January 4, 2010 Does anyone read the manual anymore? explode remember parameters. //Here's the voodoo $msg = explode(',', $input, 2); $user = trim($msg[0]); $text = trim($msg[1]); Haha, I was about to write you a sarcastic reply, but then I saw your edit. EDIT: OP, It will allow anything before or after the first comma. trim will remove the space in case they write 'ABC, message after spaces'. Beat you to it, Gizmola Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988012 Share on other sites More sharing options...
oni-kun Posted January 4, 2010 Share Posted January 4, 2010 MSG ABC, Hi how are you This will become (after trimming): $msg[0] = 'MSG ABC' $msg[1] = 'Hi how are you' Essentially in those arrays with that code. (it's 2 not one on parameter.) Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988013 Share on other sites More sharing options...
gizmola Posted January 4, 2010 Share Posted January 4, 2010 Im sorry guys i totally forgot, this text-box works as a command line so messages are initiated with a MSG command . . . . So in reality i want the user to type in the following into the text box... MSG ABC, Hi how are you Will this still work for this case? $msg = explode(',', $input, 1); $user = trim($msg[0]); $text = trim($msg[1]); Well, it seems you need to determine from your input whether it starts with the 'MSG' first. So you would have to amend the code somewhat. There are several things you could do, that are variations on what has already been explained. Truthfully it's trivial and I'd like to see you writing some code and trying some things, rather than me writing out the code for you. Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988014 Share on other sites More sharing options...
CodyBecker Posted January 4, 2010 Author Share Posted January 4, 2010 Ok, just did a str_replace("MSG", "", $user); Thanks for the help guys Quote Link to comment https://forums.phpfreaks.com/topic/187089-explode/#findComment-988018 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.