ibnclaudius Posted February 27, 2012 Share Posted February 27, 2012 How can I replace the comma from a string like this: You, like this => You like this You, Derp, like this => You and Derp like this You, Derp, Derpina, like this => You, Derp and Derpina like this You, Derp, Derpina, Derpson, like this => You, Derp, Derpina and Derpison like this Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/ Share on other sites More sharing options...
blacknight Posted February 27, 2012 Share Posted February 27, 2012 preg_replace('/^(.+)[\,]+([ \w]*)$/','\1\2',$e); shoudl do the trick will remove the last , in any statement Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321851 Share on other sites More sharing options...
PaulRyan Posted February 27, 2012 Share Posted February 27, 2012 ibnclaudius, the list you've shown us, do you have the code that creates that list? Or is this from a database as a string or scraped from a page? If it's the latter, this will work: <?PHP $string = 'You, Paul, Peter, Chris, like this'; //### Explode the string to an array $people = explode(',', $string); //### Count array length $countPeople = count($people); //### If the are 2 or more name in the array, we use the code below if($countPeople > 2) { //### Merge the last name with the "like this" text $lastName = $people[$countPeople-2].$people[$countPeople-1]; //### Assign the second last name $secondLastName = $people[$countPeople-3]; //### Remove the last 3 elements of the array array_splice($people, $countPeople-3); //### Add the new ending of the list $people[] = $secondLastName.' and '.$lastName; //### Implode the array with commas to make the list again $people = implode(',', $people); //### If there is only 1 name in the array, just remove the comma } else { //### Remove the comma from the string $people = str_replace(',','',$string); } echo $people; ?> If you have the actual code that creates the list, we could probably help you modify it to fix your issue. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321854 Share on other sites More sharing options...
ibnclaudius Posted February 27, 2012 Author Share Posted February 27, 2012 Here's my code: if ($count_school_update_likes > '0') { $likers = ''; if ($count_school_update_liked == "1") { $likers .= 'You, '; } if ($count_school_update_likes > '1') { $i = 0; foreach ($get_school_update_likes->result_array() as $liker) { $likers .= '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>, '; $i++; if ($i == 2) { $count_school_update_likes = $count_school_update_likes - 3; if ($count_school_update_likes > "0") { $likers .= ' and <a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . ' others</a> '; } break; } } } $likers .= ' like this.'; echo preg_replace("/,([^,]*)$/", " $1", $likers); //is not working correctly } Here is all the possibles outputs I would like to have: You, like this => You like this You, Derp, like this => You and Derp like this You, Derp, Derpina, like this => You, Derp and Derpina like this You, Derp, Derpina, Derpson1, Derpson2, Derpson3, Derpson4, like this => You, Derp, Derpina and other 4 like this Derp, like this => Derp like this Derp, Derpina, like this => Derp and Derpina like this Derp, Derpina, Derpson, like this => Derp, Derpina and Derpson like this Derp, Derpina, Derpson1, Derpson2, Derpson3, Derpson4, like this => Derp, Derpina, Derpson1 and other 3 like this Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321856 Share on other sites More sharing options...
Pikachu2000 Posted February 27, 2012 Share Posted February 27, 2012 Is there any reason you couldn't just load all the values into one array and do this? $last = count($likers) > 1 ? ' and ' .array_pop($likers) : ''; $likers = implode(', ', $likers); echo $likers . $last . ' like this.'; Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321859 Share on other sites More sharing options...
ibnclaudius Posted February 28, 2012 Author Share Posted February 28, 2012 No, but how am I going to do the "and 999 others like this"? Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321862 Share on other sites More sharing options...
Pikachu2000 Posted February 28, 2012 Share Posted February 28, 2012 I'm sure I can figure something out for that, but it'll have to wait until after I eat . . . Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321864 Share on other sites More sharing options...
ibnclaudius Posted February 28, 2012 Author Share Posted February 28, 2012 When i ran: $likers = $get_school_update_likes->result_array(); $lastLiker = count($likers) > 1 ? ' and ' . array_pop($likers) : ''; $likers = implode(', ', $likers); echo $likers . $lastLiker . ' like this.'; i got this: A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: libraries/action.php Line Number: 230 Array and Array like this And: print_r($likers) outputs something like this: Array ( [0] => Array ( [user_id] => 3 [user_name] => Derp ) [1] => Array ( [user_id] => 4 [user_name] => Derpina ) ) Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321868 Share on other sites More sharing options...
ibnclaudius Posted February 28, 2012 Author Share Posted February 28, 2012 A preg_replace like this would also work: if ($count_school_update_likes == '1') { //remove last comma from $likers } else if ($count_school_update_likes > '1') { //remove last comma from $likers and replace the second last comma for "and" } Or something that work on both cases... Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321870 Share on other sites More sharing options...
kicken Posted February 28, 2012 Share Posted February 28, 2012 Untested, but something like this probably would work. if ($count_school_update_likes > 0) //side note: don't quote numbers. { $likers = array; if ($count_school_update_liked == 1) { $likers[] = 'You'; } if ($count_school_update_likes > 1) { $arr = $get_school_update_likes->result_array(); $len=min(3, count($arr)); for ($i=0; $i<$len; $i++) { $liker=$res[$i]; $likers[] = '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>'; } $extra = count($arr)-$len; if ($extra > 0) { $likers[] = '<a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . ' others</a> '; } } $len=count($likers); if ($len == 1){ $likers = $likers[0].' like this'; } else if ($len > 1 ){ $last=$len-1; $list = implode(', ', array_slice($likers, 0, $last)); $likers = $list.', and '.end($likers).' like this'; } } Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321873 Share on other sites More sharing options...
jcbones Posted February 28, 2012 Share Posted February 28, 2012 What about, (keeping your same format): <?php if ($count_school_update_likes > '0') { $likers = ''; if ($count_school_update_liked == "1") { $likers[] = 'You'; } if ($count_school_update_likes > '1') { $i = 0; foreach ($get_school_update_likes->result_array() as $liker) { $likers[] = '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>, '; $i++; if ($i == 2) { $count_school_update_likes = $count_school_update_likes - 3; if ($count_school_update_likes > "0") { $likers []= ' and <a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . ' others</a> '; } break; } } } $last_likers = (isset($i) && $i > 1) ? ' and ' . array_pop($likers) : NULL; $likerstring = implode(', ',$likers) . $last_likers . ' like this.'; echo $likerstring; } Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321876 Share on other sites More sharing options...
ibnclaudius Posted February 28, 2012 Author Share Posted February 28, 2012 jcbones, I liked that you keep the format But there's some little bugs. when only the user likers, returned this: and You like this. Fixed with: $last_likers = count($likers) > 1 ? ' and ' . array_pop($likers) : ''; And when more than one user liked, returned this: You and Derp, like this. Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321879 Share on other sites More sharing options...
ibnclaudius Posted February 28, 2012 Author Share Posted February 28, 2012 And when more than one user liked, returned this: You and Derp, like this. fixed with: $likers[] = '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321882 Share on other sites More sharing options...
jcbones Posted February 28, 2012 Share Posted February 28, 2012 I was trying to use $i, I see that didn't work, and yes, I missed that comma. Didn't notice it until you pointed it out. Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321883 Share on other sites More sharing options...
ibnclaudius Posted February 28, 2012 Author Share Posted February 28, 2012 Thanks, all! I made some changes, almost everything is correct now, the only problem is when there's only one user that liked, his name or you do not output. if ($count_school_update_likes > 0) { $likers = array(); if ($count_school_update_likes > 1) { $i = 0; if ($count_school_update_liked == 1) { $likers[] = 'Você'; $i = 1; } if ($get_school_update_likes) { foreach ($get_school_update_likes->result_array() as $liker) { $likers[] = '<a id="' . $liker['user_id'] . '" href="' . $liker['user_id'] . '">' . $liker['user_name'] . '</a>'; $i++; if ($i == 3) { $count_school_update_likes = $count_school_update_likes - 3; if ($count_school_update_likes > 0) { $likers[] = 'mais <a id="stream-updates-item-likes-view" href="#">' . $count_school_update_likes . '</a> '; } break; } } } } $last_liker = count($likers) > 1 ? ' e ' . array_pop($likers) . ' curtiram' : ' curtiu'; $likers = implode(', ', $likers) . $last_liker . ' isto'; echo $likers; } Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321892 Share on other sites More sharing options...
ibnclaudius Posted February 28, 2012 Author Share Posted February 28, 2012 Fixed: if ($count_school_update_likes >= 1) Thanks, all! Quote Link to comment https://forums.phpfreaks.com/topic/257893-replace-comma-from-string/#findComment-1321914 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.