Jump to content

Explode


CodyBecker

Recommended Posts

Hi all,

 

(Im new to this so go easy on me  :shrug: )

 

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.

Link to comment
Share on other sites

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]);

 

Link to comment
Share on other sites

Does anyone read the manual anymore?  :oexplode 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'.

Link to comment
Share on other sites

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.  ;)

Link to comment
Share on other sites

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]);

Link to comment
Share on other sites

Does anyone read the manual anymore?  :oexplode 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  ;)

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.