Re: RIGHT Function?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



sono-io@xxxxxxxxxxxxx wrote:
Jim,

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


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 ) {
    $dirty_price = $parts[(count($parts)-1)];
} else {
    $dirty_price = $parts[0];
}

$clean_price = number_format($dirty_price, 2);

?>


Any 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



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux