0

When I try to convert a number as a string to an integer, The value changes to 0.

I did a preg_match and picked out a number value which as an output of preg_match is in a string datatype.

That is, When i tried to extract the number 7123 in the following xml data, <Count> 7123 </Count> using preg_match, I got the value 7123 as a string. [Used gettype function]. When I am checking if its 0 or anyother value, I need to convert it to integer datatype just to compare the values farely. So i do intval("7123") or intval($count) to get the integer value. But when I echo the value, its 0. Read few blogs online and one said this type of a thing happens when the string value is non-printable. Can someone help me understand this and help me solve it?

Vignesh
  • 912
  • 6
  • 13
  • 24

1 Answers1

2

It's very simple once you know how to convert a string to integer:

$str = "7123";
$val = (int) $str;

echo $val + 1;

result: 7124

This is not the only way to convert strings to integers, but it's been proven to be the fastest.

Community
  • 1
  • 1
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Nope. It still gives me the same 0 as result – Vignesh Jun 05 '14 at 18:31
  • @Vignesh, What is the output if you `echo var_dump($count);` or the variable you are setting? (do it before trying to do the conversion). – l'L'l Jun 05 '14 at 18:35
  • The output is `int(0)` – Vignesh Jun 05 '14 at 18:37
  • With `(int)$str` step, the result was `int(0)`. Without that it is `string(24) "48"` – Vignesh Jun 05 '14 at 18:41
  • This is the code: `preg_match("/(\S+)<\/Count>/i",$handle,$match1); $totalcount=$match1[1];` $handle has the xml data and $match1 is the array of results. The value is in $totalcount. When I gave `echo var_dump($totalcount)` The result was `string(24) "48"` – Vignesh Jun 05 '14 at 18:43
  • 1
    Change your regex pattern to this: `preg_match("/(\d+).+\/Count>/i",$handle,$match1);` – l'L'l Jun 05 '14 at 18:48
  • If there is a value present it picks up the value. But if there is 0 in between the Count tags, It picks up a random value of 55. Dont know why – Vignesh Jun 05 '14 at 18:58
  • This is the sample data ` 0 0 0 PONT[tiab] tiab 552 N Carboplatin[tiab] tiab 10551 N AND PONT[tiab] AND Carboplatin[tiab] – Vignesh Jun 05 '14 at 19:02
  • 1
    Okay, I see what is going on. Are you only trying to capture the first value? also, post your code in your question next time. – l'L'l Jun 05 '14 at 19:08
  • Yes I am.. the first count value. Sorry. will post the code as well. – Vignesh Jun 05 '14 at 19:13
  • 1
    Change your regex to this: `preg_match_all("//i",$handle,$match1);` — It will grab all values in an array, but you can use it just as you did before, with one slight change `$totalcount=$match1[1][0];` – l'L'l Jun 05 '14 at 19:23
  • Thanks. When I try printing the array using a forloop, I get this as n error. `Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/search.php on line 83 Array ` – Vignesh Jun 05 '14 at 19:26
  • `preg_match_all("//i",$handle,$match1); for ($i=0; $i – Vignesh Jun 05 '14 at 19:27
  • 1
    You're missing a semi-colon after `echo $match1[$i]`; but since it's an array you need `print_r($match1[$i]);` you can also do `print_r($match1[1]);` without the loop, it gives the same result. – l'L'l Jun 05 '14 at 19:33
  • I'm having the same problem `string to int casting returns 0` and have no clue how to handle it. The above solution just adds 1 to existing 0. – Naveed Abbas Mar 11 '18 at 07:12
  • @ToughGuy: Post a new question with your problem and someone will surely be able to tell you what is going on... – l'L'l Mar 11 '18 at 07:43