Re: RIGHT Function?
| From: | Jim Lucas | Date: | Sat, 14 Nov 2009 02:34:18 +0000 |
| Subject: | Re: RIGHT Function? | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-299821@lists.php.net to get a copy of this message | ||
sono-io@fannullone.us wrote:
Jim,Basically, you are using an if-then-else statement. Read here: http://us2.php.net/manual/en/control-structures.alternative-syntax.php As for the PHP_EOL: Read here: http://us2.php.net/manual/en/reserved.constants.php and search for PHP_EOL You basic problem with the PHP_EOL is that when echo'ed out, it represents a \n character. The value you are working with might be \n\r or just \r Others: correct me if I'm wrong, but... Linux, BSD, etc... use \n as line endings Windows (All versions) use \r\n Mac (Old school) used \r Mac (Current) BSD Style \n But, with all that said. Here is the code a little further broken out. <?php $parts = preg_split('|[\n\r]+|', $item['unitprice']); if ( count($parts) > 1 ) {$parts = explode(PHP_EOL, $item['unitprice']); $price = '$'.(( count($parts) > 1 ) ? $parts[0] : $parts[(count($parts)-1)]);For some reason, I couldn't get explode to work with PHP_EOL. $parts[0] would return the entire field, so apparently it wasn't "exploding". So I tried exploding on the ')' instead, which worked, but the return character that's after the ')' was included in the output, i.e.:$ 6.56 so I added 'trim' which took care of that. I also had to use 'number_format' again, since there are exact dollar amounts like 413. Here's what ended up working for me: $parts = explode(')', $item['unitprice']); $price = '$'.number_format(trim((( count($parts) > 1 ) ? $parts[(count($parts)-1)] : $parts[0])),2);
$dirty_price = $parts[(count($parts)-1)];} else {
$dirty_price = $parts[0];} $clean_price = number_format($dirty_price, 2); ?>
-- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William ShakespeareAny idea why PHP_EOL didn't work? If I could get it to work, I could remove the trim function and 2 of those parentheses, which would look a lot nicer.Thanks again, Frank