在php中将字符串转换为整数时返回零

在php中将字符串转换为整数时返回零

问题描述:

when I want to convert a numeric string variable into integer in php, the return value is zero. I used all ways to convert it! such as:(int), (integer), intval(), settype(), eval(), etc. all of them return zero!

var_dump($myVariable) prints the following:

string(4) "۲۹"

当我想在php中将数字字符串变量转换为整数时,返回值为零。 我用各种方法来转换它! 例如:(int),(整数),intval(),settype(),eval()等等都返回零! p>

var_dump($ myVariable) code>打印以下内容: p>

  string(4)“29”
   code>  pre> 
  div>

I found it! before converting string to integer, I should convert arabic number to english using this function:

function convert($string) {
$persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
$num = range(0, 9);
return str_replace($persian, $num, $string);
}

now, I can convert it! thanks from kingkero.

Other common error is trying to cast a string variable that contains numbers but starts with zero, i.e.:

string(5) " 358"

To solve this, I used a filter_var sanitizing number INT doing this:

$myVariable = filter_var($myVariable, FILTER_SANITIZE_NUMBER_INT);