cast integer php - remove symbol from string - php

I have this code and i make a cast to remove the symbol €.
$t = "€2000";
$venc = (int)$t;
echo $venc; // actually echo is 0 and i want 2000 (remove symbol)
The output is 0 and not 2000, so, the code is not working as i expect.
What is the reason for (int)$t; not echo 2000 ?
thanks

casting routine does not remove invalid characters, but start from the beginning and stops when first invalid character is reached then convert it to number, in your case Euro sign is invalid and it is the first character thus resulting number is 0.
check http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
you could try (int)preg_replace('/\D/ui','',$t);
however if you are dealing with currencies you should not forget that they are not integers but floats
(float)preg_replace('/[^0-9\.]/ui','',$t);

This can help you
$t = preg_replace('/[^0-9]/i', '','€2000');
$venc = (int)$t;
echo $venc;

$t = "€2000";
$venc = (int)substr($t,1);
echo $venc;

use substr
echo substr($t,3); // returns "2000"

Related

Change a string into an int

In my store i am trying to change a String into an integer the code which I am using is:
<?php echo $_excl; ?>
<?php $int_excl = intval($_excl);?>
<?php var_dump ($int_excl);?>
When i var_dump $int_excl is says the value is 0 when it should be 4.99 .
What am I doing wrong?
var_dump ($_excl) returns string(33) "£4.99"
Both floatval and intval methods start to look for numbers from the beginning of the string (removing the preceding whitespace) - and stop looking at the first symbol that cannot be a part of the number. In your case, it's the very first one - £. That's why intval just returns 0 (as floatval does).
One way to resolve this is to remove all the non-digit symbols at the beginning of your string with preg_replace:
$excl = '£4.99';
$value = preg_replace('/^\D+/u', '', $excl);
var_dump(intval($value)); // int(4)
var_dump(floatval($value)); // float(4.99)
Demo. This approach is change-resistant - it works (without any modifications) if £ is replaced with $ or any other currency symbol, or dropped altogether.
<?php
$_excl = '4.99' ;//assuming
echo $_excl;
$int_excl = floatval($_excl);
var_dump ($int_excl);
?>
Use above code, since $_excl = 4.99 which is a float value, intval() would return 4.
This should work for you:
(You have to remove the character £ from the string)
<?php
$_excl = "£4.99";
$_excl = str_replace("£", "", $_excl);
$int_excl = floatval($_excl);
var_dump ($int_excl);
?>
Output:
float(4.99)
You are parsing float values, and therefore need floatval() PHP Float Manual.
If intval() returns 0, it means that the conversion failed. Source: PHP Intval)
In your case, the conversion fails because the string doesn't start with a number, but with a currency symbol.
This should solve your problem:
<?php
echo $_excl;
$int_excl = floatval(ltrim($_excl, '£'));
var_dump ($int_excl);
?>
(ltrim removes the first character if it is £)

PHP Multiplication not working

$databtcguild = file_get_contents('http://btcguild.com');
preg_match('~<b>Pool Speed</b></a> (.*?) TH/s~',$databtcguild,$btcguild);
$btcguildhashrategh = ($btcguild[1] * 1000);
echo $btcguildhashrategh;
echo "<br>";
echo $btcguild[1];
For some reason this code is outputting the wrong answer. For example, $btcguild[1] will equal 12,747 and this code will output 12000. I'm completely lost here. Thanks for any help.
The "hash speed" value you are extracting from that site has a value with a comma in it:
12,747
PHP needs to convert this string to a numeric value, and the comma causes the numeric value 12 to be returned (, is interpreted as a decimal)
Make sure you strip all non-numeric characters before multiplying:
//keep only values 0-9 and decimal (period)
$hash_speed = preg_replace("/[^0-9.]/", "", $btcguild[1]);
$btcguildhashrategh = ($hash_speed * 1000); //returns 12747000
Try explicitly type-casting the result: $btcguildhashrategh = ((double)$btcguild[1] * 1000);
Otherwise PHP will convert it into an int.

PHP - Read TXT from specific position

I'm having trouble with PHP text parsing
I have a txt file which has this kind of information:
sometext::sometext.0 = INTEGER: 254
What i need is to get the last value of 254 as variable in PHP.
in this txt file this last value can change from 0 to 255
"sometext::sometext.0 = INTEGER: " this part doesn't change at all.
It has a length of 36 symbols, so i need get with PHP what is after 36 symbol into variable.
Thank you.
Try using sscanf:
$string = "sometext::sometext.0 = INTEGER: 254";
sscanf($string, "sometext::sometext.0 = INTEGER: %d", $number);
echo $number;
Demo: http://codepad.org/Ash2QHvI
Perhaps substr() will do?
$text = "sometext::sometext.0 = INTEGER: 254";
print substr($text, 37);
See it in action here (adjusted to match your sample data): http://codepad.org/5Ikt3kRh
Try fgets for reading a file.
For the parsing I use split. I wrote an example here. But sscanf seems to be the better option.
Since you know the string is always going to be of that form you can use the substr() function to extract the part of the string you want. Then use intval() to make it an integer if needed.
Note: My count shows that the number you're trying to get starts at the 33rd position (which becomes 32 below, as substr is 0 based), not the 36th.
You can get what you want with:
$the_str = 'sometext::sometext.0 = INTEGER: 254';
$num = intval(substr($the_str, 32));

check for number anywhere in a string

$vari = "testing 245";
$numb = 0..9;
$numb_pos = strpos($vari,$numb);
echo substr($vari,0,$numb_pos);
The $numb is numbers from 0 to 9
Where am I wrong here, all I need to echo is testing
You want to cut out the numbers from a string?
$string = preg_replace('/(\d+)/', '', 'String with 1234 numbers');
Use a regular expression to strip numeric characters from your string.
or, use a regular expression to find the first instance of one either way...
Your code won't work as-is, as it'll fail if the number if the first character in the string. (You need to check $numb_pos !== false prior to the substr.)
Irrespective, if you just want to check for the existance of a number in a string, something like the following would probably be more efficient.
$digitMatched = preg_match('/\\d/im', $vari);

Removing leading 0?

Is there a simple way to remove a leading zero (as in 01 becoming 1)?
You can use the ltrim function:
ltrim($str,"0");
$str = "01";
echo intval($str);
if you use the trim functions, you might mistakenly remove some other character, like by triming "12" your will have "2".
use the intval() function. this function will convert your string (which could start by a leading zero or not) to an integer value. intval("02") will be 2 and intval ("32") wll be 32.
Regex replace /^0*/ with '' for a string return solution
The exact code will be something like this
<?php
$string_number = '000304';
echo preg_replace('/^0*/', '', $string_number);
?>
Just multiply by 1
echo "01"*1

Categories