iChriss Posted February 19, 2012 Share Posted February 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257293-sending-to-multiple-users/ Share on other sites More sharing options...
MMDE Posted February 19, 2012 Share Posted February 19, 2012 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/257293-sending-to-multiple-users/#findComment-1318840 Share on other sites More sharing options...
iChriss Posted February 19, 2012 Author Share Posted February 19, 2012 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/257293-sending-to-multiple-users/#findComment-1318841 Share on other sites More sharing options...
MMDE Posted February 19, 2012 Share Posted February 19, 2012 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/257293-sending-to-multiple-users/#findComment-1318843 Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2012 Share Posted February 19, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/257293-sending-to-multiple-users/#findComment-1318845 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.