Re: [PHP] RIGHT Function?
On Nov 13, 2009, at 6:34 PM, Jim Lucas wrote: 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 I read the links you sent, plus some others. It took me awhile, but I get it now. If the "return character" that was entered into that field is not the same as what PHP_EOL is looking for, it won't work. $parts = preg_split('|[\n\r]+|', $item['unitprice']); preg_split works perfectly! Very similar to Perl's split function. Here's what I have now: $parts = preg_split('|\s+|', $item['unitprice']); $price = '$'.number_format((count($parts) > 1) ? $parts[(count($parts)-1)] : $parts[0],2); Clean and concise. Thanks a million, Jim! I really appreciate your helpful responses. Thanks again, Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
sono...@fannullone.us 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. 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
Re: [PHP] RIGHT Function?
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); 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
Hi Jim, $parts = explode(PHP_EOL, $item['unitprice']); $price = '$'.(( count($parts) > 1 ) ? $parts[0] : $parts[(count($parts)-1)]); Thanks for the code! After reading up on PHP_EOL and explode, I now understand what you've done. However, can you tell me why you like this better? Is it because it is cleaner without the if/else statements, or is there more to it than that? Regards, Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
sono...@fannullone.us wrote: > I think I've solved a problem that I had posted back in September. > Here's a recap: > > == > I need to grab a dollar amount from a text field in a MySQL db that > can contain more information than just the price. Here are 4 examples > of what could be in that field: > > 48,(min) > 2.66 > > 24,(min) > 10.50 > > 4,(min) > 104.82 > > 98.56 > > If there is more info in that field than just the price (as in the > first 3 examples), the price is always on the 2nd line. > == > > The following code works with the tests I've given it so far, but I > just want to double check before I go live with it: > > ... > if ($position = strpos($item['unitprice'], ')') ) > $price = "$" . number_format(substr($item['unitprice'], $position + > 1),2); > else > $price = "$" . number_format($item['unitprice'],2); > ... > Well, when you put it that way, I would try this. ... $parts = explode(PHP_EOL, $item['unitprice']); $price = '$'.(( count($parts) > 1 ) ? $parts[0] : $parts[(count($parts)-1)]); ... > Legend: > $item['unitprice'] is coming from a MySQL statement > > I'm grabbing the position of the right parentheses and adding 1 to > it. Then the substr grabs everything from that point to the end of the > string, correct? If there isn't a ")" in the field, then the else > statement should be performed. > > Can anyone see any errors in my code? Would there be a better way > to write this? > > Thanks again, > Frank > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
I think I've solved a problem that I had posted back in September. Here's a recap: == I need to grab a dollar amount from a text field in a MySQL db that can contain more information than just the price. Here are 4 examples of what could be in that field: 48,(min) 2.66 24,(min) 10.50 4,(min) 104.82 98.56 If there is more info in that field than just the price (as in the first 3 examples), the price is always on the 2nd line. == The following code works with the tests I've given it so far, but I just want to double check before I go live with it: ... if ($position = strpos($item['unitprice'], ')') ) $price = "$" . number_format(substr($item['unitprice'], $position + 1),2); else $price = "$" . number_format($item['unitprice'],2); ... Legend: $item['unitprice'] is coming from a MySQL statement I'm grabbing the position of the right parentheses and adding 1 to it. Then the substr grabs everything from that point to the end of the string, correct? If there isn't a ")" in the field, then the else statement should be performed. Can anyone see any errors in my code? Would there be a better way to write this? Thanks again, Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
At 12:33 PM -0700 9/24/09, sono...@fannullone.us wrote: On Sep 24, 2009, at 12:15 PM, Jay Blanchard wrote: substr will work from right to left. If your data is in a variable do this; Thanks, Jay. That does the job. Frank Frank: I came to this thread a little late, but the following are some functions I use, namely right(), left() and mid(). These were built-in functions in different language I used many years ago. They just seemed natural to me so I wrote them for php. Cheers, tedd - // == returns the middle number of characters from a string starting from the left // $string = "123456789" // mid($string, 3, 4) returns "4567" function mid($string, $left_start, $length) { $str = substr($string, $left_start, $length); return $str; } ?> -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
On Sep 24, 2009, at 12:15 PM, Jay Blanchard wrote: substr will work from right to left. If your data is in a variable do this; Thanks, Jay. That does the job. Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
- Original Message > From: Tommy Pham > To: PHP General List > Sent: Thursday, September 24, 2009 12:19:51 PM > Subject: Re: [PHP] RIGHT Function? > > - Original Message > > From: Jay Blanchard > > To: sono...@fannullone.us; PHP General List > > Sent: Thursday, September 24, 2009 12:15:13 PM > > Subject: RE: [PHP] RIGHT Function? > > > > [snip] > > I have a price field that sometimes contains entries like this: > > > > 250,(min) > > 5.32 > > > > How can I pull just the price ($5.32 in this example) from this > > field > > to display on a web page? Is there a RIGHT function in PHP? When I > > search the PHP site, I'm being told there isn't one. > > > > Here's the code I've been using: > > [/snip] > > > > substr will work from right to left. > > > > If your data is in a variable do this; > > > > echo substr($myData, -5); > > > > Why -5? To account for 2 decimal places, the decimal and up to 99 > > dollars? > > > > http://www.php.net/substr > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > http://dev.mysql.com/doc/refman/5.1/en/string-functions.html If you want to retain the ',(min)' for other uses, you'll have to do it in PHP http://www.php.net/manual/en/ref.strings.php stripos() and substr() will do it if you don't want to deal with/learn regex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
- Original Message > From: Jay Blanchard > To: sono...@fannullone.us; PHP General List > Sent: Thursday, September 24, 2009 12:15:13 PM > Subject: RE: [PHP] RIGHT Function? > > [snip] > I have a price field that sometimes contains entries like this: > > 250,(min) > 5.32 > > How can I pull just the price ($5.32 in this example) from this > field > to display on a web page? Is there a RIGHT function in PHP? When I > search the PHP site, I'm being told there isn't one. > > Here's the code I've been using: > [/snip] > > substr will work from right to left. > > If your data is in a variable do this; > > echo substr($myData, -5); > > Why -5? To account for 2 decimal places, the decimal and up to 99 > dollars? > > http://www.php.net/substr > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php http://dev.mysql.com/doc/refman/5.1/en/string-functions.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] RIGHT Function?
[snip] I have a price field that sometimes contains entries like this: 250,(min) 5.32 How can I pull just the price ($5.32 in this example) from this field to display on a web page? Is there a RIGHT function in PHP? When I search the PHP site, I'm being told there isn't one. Here's the code I've been using: [/snip] substr will work from right to left. If your data is in a variable do this; echo substr($myData, -5); Why -5? To account for 2 decimal places, the decimal and up to 99 dollars? http://www.php.net/substr -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] RIGHT Function?
Original Message > From: "sono...@fannullone.us" > To: PHP General List > Sent: Thursday, September 24, 2009 12:01:41 PM > Subject: [PHP] RIGHT Function? > > Hi all, > > I could use some help. I've scoured both the MySQL and PHP sites that I > know of and can't find an answer to this. > > I have a price field that sometimes contains entries like this: > > 250,(min) > 5.32 regex? If 250,(min)5.32 is in your row data, how do you sort price ascending/descending? > > How can I pull just the price ($5.32 in this example) from this field to > display on a web page? Is there a RIGHT function in PHP? When I search the > PHP > site, I'm being told there isn't one. > > Here's the code I've been using: > > > itemid='WR-1240'",$db); > printf('List: $%s ', number_format(mysql_result($result,0,"priceList"),2)); > printf('Your Price: $%s', > number_format(mysql_result($result,0,"unitprice"),2)); > ?> > > Thanks, > Frank > > --PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] RIGHT Function?
Hi all, I could use some help. I've scoured both the MySQL and PHP sites that I know of and can't find an answer to this. I have a price field that sometimes contains entries like this: 250,(min) 5.32 How can I pull just the price ($5.32 in this example) from this field to display on a web page? Is there a RIGHT function in PHP? When I search the PHP site, I'm being told there isn't one. Here's the code I've been using: WHERE itemid='WR-1240'",$db); printf('List: $%s', number_format(mysql_result($result, 0,"priceList"),2)); printf('Your Price: $%s', number_format(mysql_result($result, 0,"unitprice"),2)); ?> Thanks, Frank -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php