473,414 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to convert string to integer with (int)/intval?

Hi there,

gush, frustrating. I am not a pro; but I know PHP well enough to understand how it works. And a simple conversion from string to integer via (int)$value or intval($value) should be straightforward. But it's not working in my case.

I have a string:
Expand|Select|Wrap|Line Numbers
  1. % population: 99%
which holds some information I extract via preg_match.

The resulting variable $r[0] is "99". I have an
Expand|Select|Wrap|Line Numbers
  1. if (($r[0] >= 75))
which used to work. But I don't know for what reason, that doesn't do it anymore.

I tried all combinations of (int) and intval()
Expand|Select|Wrap|Line Numbers
  1. $t = (int)$r[0];
  2. $t = intval($r[0];
, but in vain. It treats my variable afterwards as 0.

What is wrong here?!

Thanks for any hints!
Nov 30 '10 #1

✓ answered by Dormilich

what does this look like in the (HTML) source code?

EDIT: do you use UTF-8? try running htmlentities() (with the appropriate encoding) on it.

11 6114
Dormilich
8,658 Expert Mod 8TB
depending on your RegEx, $r[0] might not be "99" any more.
Dec 1 '10 #2
Thanks. But I checked that, and the value of $r[0] is "99". But it won't convert into a number.
Dec 1 '10 #3
Dormilich
8,658 Expert Mod 8TB
what does var_dump($r[0]); give you?
Dec 1 '10 #4
I am not in the office right now. Will check tomorrow. Thanks already for the hints!
Dec 1 '10 #5
Ok, so, it gives me a
Expand|Select|Wrap|Line Numbers
  1. string(5) "99"
. What does that mean? That the value is 5 characters long? Why would that be?
Dec 2 '10 #6
Dormilich
8,658 Expert Mod 8TB
exactly, you could have some whitespace before and/or after. you could try trimming the value beforehand.

PS. verify the output in the (HTML) source, as HTML itself trims whitespace in its rendering.
Dec 2 '10 #7
I did try as well the "trim" the other day, but nothing did change...
Dec 2 '10 #8
Dormilich
8,658 Expert Mod 8TB
well, you need to find out, why the string length is given as 5.
Dec 2 '10 #9
Strange, strange. I used a trim, but nothing changed. It seems that when the number has two places (i.e >= 10), it turns from string(4) to string(5).

string(4) "9"
string(5) "22"

Perhaps a clue for someone? I am really stuck. No idea what to do.

Thanks a lot for any hints!
Dec 6 '10 #10
Dormilich
8,658 Expert Mod 8TB
what does this look like in the (HTML) source code?

EDIT: do you use UTF-8? try running htmlentities() (with the appropriate encoding) on it.
Dec 6 '10 #11
Ha, jackpot! And I feel stupid that I haven't looked in the HTML code myself before. Indeed, there is a "<b>9" and "<b>22", instead of the pure number. Thanks a thousand times for this hint! Great!!
Dec 7 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jake | last post by:
Is there any way (other then using a loop) to convert a string array to an int array? Any help would be great. thanks :// Jake
24
by: djozy | last post by:
Please, how can I convert selected item from dropdown list to integer? Thank you
5
by: nbohana | last post by:
I have tried several methods in converting my string data to an int. I am getting a message back 'Input string was not in a correct format' strSQL1 = "SELECT FROM WHERE = " +...
3
by: clickon | last post by:
I have an integer value in a textbox, i want to use the value to perform a mathematical calculation, however i can only access this value as a string and i can't cast a string to an int. How do i...
1
by: Divvy Ballacks | last post by:
How do I convert a C++ string (as in "string thingy;" as opposed to "char thingy;") to an integer? I know how to use C atoi() and strtol() but I get an compile error. Can anyone help? The simple...
7
by: tereglow | last post by:
Hello, I am a complete newbie to Python and am accustomed to coding in PHP/ Perl/Shell. I am trying to do the following: I have a string: cpuSpeed = 'Speed: 1000000000' What I would...
13
by: deepthisoft | last post by:
hai, I want to convert String to int.My coding is like this, rs=stmt.executeQuery("select a from mailtable"); rs.absolute(-1); String id=rs.getString("a"); System.out.println(id);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.