How to trim unnecessary white space in PHP*
The problem is this. U have 2 if clauses that output string, or an empty string, and I need to put them in a class later on:
<?php
$result1 = ($result_1) ? 'first' : '';
$result2 = ($result_2) ? 'second' : '';
$class_out = trim('clear' . ' ' .$result1. ' ' .$result2);
return '<span class="'.$class_out.'"></span>';
?>
Now, $result_1
and $result_2
, can be set, or not, so the possibilities of output should be
<span class="clear first second"></span>
<span class="clear first"></span>
<span class="clear second"></span>
<span class="clear"></span>
But instead they are
<span class="clear first second"></span>
<span class="clear first"></span>
<span class="clear second"></span><!-- EXTRA SPACE-->
<span class="clear"></span>
Now, this isn't a fault, but it looks kinda ugly. Why have an extra space if it can be avoided? The issue is, how? I found implode
function, but that only glues things together, I don't know if it will trim the unnecessary space. What should I use?