How do I remove the space before the first echoed element?

	$monkeys = array(
		'species' => "baboon",
		'tree' => "cedar",
		'food' => "mango",
		'drink' => "whiskey"
	);
	
	
	$monkeystr = array();
	foreach ($monkeys as $monkey){
		$monkeystr[] = " $monkey";
	}
	$resultA = implode(",",$monkeystr);
	echo $resultA;

Open in new window


Try the output and you will see:
" baboon, cedar, mango, whiskey"

I'd like to remove the space before "baboon"

thanks
badwolfffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Maybe something like this...
http://iconoun.com/demo/temp_badwolff.php
<?php // demo/temp_badwolff.php

/**
 * http://www.experts-exchange.com/questions/28787599/How-do-I-remove-the-space-before-the-first-echoed-element.html
 */
error_reporting(E_ALL);


$monkeys = array(
		'species' => "baboon",
		'tree' => "cedar",
		'food' => "mango",
		'drink' => "whiskey"
	);


$monkeystr = implode(', ', $monkeys);
echo htmlentities($monkeystr);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark BradyPrincipal Data EngineerCommented:
or you can just trim the result.
echo trim($resultA);
// trims space on either side of the string.

// you could also use ltrim($resultA) to trim only the start of the string or rtrim() to trim space of the end of a string.

Open in new window

0
badwolfffAuthor Commented:
i tried the trim and ltrim - don't work in this example
did you try it?
thanks
0
Peos JohnPHPCommented:
Please try if the following works for you... It removes first nbsp space ....

<?php // demo/temp_badwolff.php

$monkeys = array(
            'species' => "baboon",
            'tree' => "cedar",
            'food' => "mango",
            'drink' => "whiskey"
      );
      
      
      $monkeystr = array();
      foreach ($monkeys as $monkey){
            $monkeystr[] = "&nbsp;$monkey";
      }
      $resultA = implode(",",$monkeystr);
      
      
      $str2 = substr($resultA, 6);
      echo $str2;
      
?>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.