在类型转换php中获取错误?

I stored a Javascript value to a PHP variable. When I use var_dump to print it, var_dump returns int(0). It should display int(10). I am using this code:

<script type="text/javascript">
        var a = "Hello world: 12345";
        var b = a.replace ( /[^\d.]/g, '' );
    </script>
    <?php
        $identity = '<script type="text/javascript">document.write(b)</script>';
        var_dump($identity);
        echo "<br/>";
        $identity  = preg_replace('/[^\d]/', '', $identity ); //removes everything except digits
        $ord = (int)$identity;
        var_dump($ord);
    ?>

Where have I gone wrong?

Where have i gone wrong ?

  1. JavaScript code doesn't evaluate inside a php script, pretty basic.
  2. You're trying to convert a string to int but php won't allow you to do that when the string contains letters, or anything different from digits.

If you use:

$identity = '<script type="text/javascript">document.write(10)</script>';
$identity  = preg_replace('/[^\d]/', '', $identity ); //removes everything except digits
$ord = (int)$identity;
var_dump($ord);

php will convert the string to int without errors.