PHP Strip String, Convert to int -
i have string $special
formatted £130.00
, ex tax(vat) price.
i need strip first char can run simple addition.
$str= substr($special, 1, 0); // strip first char '£' echo $str ; // echo value check worked $endprice = (0.20*$str)+$str ; // work out vat
i don't receive value when echo on sec line ? need convert string integer in order run add-on ?
thanks
matt
+++ update
thanks help this, took code , added of own, there more nicer ways works :) found out if cost below 1000 £130.00 if cost larger value include break. ie £1,400.22.
$str = str_replace('£', '', $price); $str2 = str_replace(',', '', $str); $vatprice = (0.2 * $str2) + $str2; $display_vat_price = sprintf('%0.2f', $vatprice); echo "£"; echo $display_vat_price ; echo " (inc vat)";
thanks again, matt
you cannot utilize substr
way using currently. because trying remove £
char, two-byte unicode character, substr()
isn't unicode safe. can either utilize $str = substr($string, 2)
, or, better, str_replace()
this:
$string = '£130.00'; $str = str_replace('£', '', $string); echo (0.2 * $str) + $str; // 156
original answer
i'll maintain version still can give insight. reply ok if £
wouldn't 2byte unicode character. knowing this, can still utilize need start sub-string @ offset 2
instead of 1
.
your usage of substr
wrong. should be:
$str = substr($special, 1);
check documentation 3rd param length of sub-string. passed 0
, hence got empty string. if omit 3rd param homecoming sub-string starting index given in first param until end of original string.
php string int
No comments:
Post a Comment