04-08-2008, 12:56 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Posts: 78
Thanks: 0
|
Looking for a better way to code this
I'm building a string dynamically. The result is that it may start with a dash (-), a space or a letter. I need to remove the first two, if they occur. The following code simulates the problem. If ran as is, it outputs XB - CX, which is correct (the X's are to show there aren't any spaces). But it seems unnecessary and overly complicated to use three trims in a row. The $array can come from a number of sources so while I could probably condition the string there, that would cause more code than the three trims. Can anyone see a better way to do this?
PHP Code:
$array = array();
$array['a'][0] = '';
$array['a'][1] = ' B';
$array['a'][2] = 'C';
$str = implode(' ' .' - ' . ' ', $array['a']);
$str = trim($str);
$str = ltrim($str, '-');
$str = ltrim($str);
echo 'X'.$str.'X';
|
|
|
|