Jump to content

Sending to multiple users


iChriss

Recommended Posts

Hey guys.

 

So I'm about to start developing a Private Messaging system for a CMS that I already have set up and working fine, and I had a problem I would like to solve before I start. I would like to add a feature that allows users to send the message to one or more users at a time. Whether it be by typing in the different usernames seperated by commas in the input field or another method, I have no idea how I'd handle submitting this into the database.

 

I don't want it to be like a group conversation though, I want it to submit the message seperately for each user they included in the receptitents field.

 

Any suggestions on how to go about doing this?

Gathering it'd be like an array of some sort but I have very little experience with arrays from forms and how to seperate them.

Link to comment
Share on other sites

<?php
$names = 'jack, john, robert';
$pattern = '/,\s*/';
$array_of_names = preg_split($pattern,$names);
foreach($array_of_names AS $name){
echo $name . '<br />';
}
?>

Does this help you in any way?

 

EDIT:

I will post a better script.

Link to comment
Share on other sites

<?php
$names = 'jack, john, robert';
$pattern = '/,\s*/';
$array_of_names = preg_split($pattern,$names);
foreach($array_of_names AS $name){
echo $name . '<br />';
}
?>

Does this help you in any way?

 

EDIT:

if the username doesn't contain any spaces:

<?php
$names = '    jack,     john  , robert   ';
$array_of_names = preg_split('/,\s*/', $names);
foreach($array_of_names AS $name){
$name = preg_replace('/\s*/', '', $name);
echo $name . '<br />';
}
?>

 

I think that is exactly what I need!

Thanks heaps.

Link to comment
Share on other sites

<?php
$names = '    Michael Jackson ,   Ingmar Bergman  , Jeffrey Dahmer  ';
$array_of_names = preg_split('/,/', $names);
$amount_of_names = count($array_of_names);
for($i=0; $i<$amount_of_names; $i++){
$array_of_names[$i] = preg_replace('/^\s*/', '', $array_of_names[$i]);
$array_of_names[$i] = preg_replace('/\s*?$/', '', $array_of_names[$i]);
}
foreach($array_of_names AS $name){
echo $name . '<br />';
}
?>

Now the usernames can contain spaces as well, and there can be spaces all over the place. lol

Link to comment
Share on other sites

There's really no need for a regex pattern at all. You can just explode on the comma, and trim the array elements.

 

$names = '    Michael Jackson ,   Ingmar Bergman  , Jeffrey Dahmer  ';
$array_of_names = explode(',', $names);
$array_of_names = array_map('trim', $array_of_names);

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.