4

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) "۲۹"
4
  • 1
    Can you post the string you are casting/converting?
    – kero
    Commented Mar 14, 2015 at 10:39
  • it is return value of a function this is its link: jdf.scr.ir/download/dl.php the variable I used is this: $month_length = jdate('t'); I want to convert $month_length to integer and use it as index of a for loop
    – AzizAhmad
    Commented Mar 14, 2015 at 10:44
  • This does not help at all. Please do a var_dump($month_length) and post the result in your question
    – kero
    Commented Mar 14, 2015 at 10:45
  • string(4) "۲۹" oh! I think this is because the return value is UTF8 ! how can I solve it?
    – AzizAhmad
    Commented Mar 14, 2015 at 10:51

2 Answers 2

9

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

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.