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
https://forums.phpfreaks.com/topic/257293-sending-to-multiple-users/
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.

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.