Jump to content

replace comma from string


ibnclaudius
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';
}
}

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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