0

To start, I could not find this answer online because of the way my variable string is defined. Normally I should be able to add 0 to the variable, or use (int), but it does not work.

<?php

$casestringid = "'118'";
$caseid = $casestringid + 0;

echo $casestringid;

echo $caseid;

?>

Output: '118'0

As you can see, because of the way my first variable is declared, the standard methods of converting a string to an integer does not work. My $casestringid is written like that because it requests a number from another page. Rather than trying to change how to format that, I figure it will be easier for help on how to convert a string that looks like that, into an integer. I would like the output of caseid to be 118. Thanks for any help in advance.

1
  • Replace (remove) the single quotes first then type cast as integer? Commented Aug 6, 2013 at 18:38

7 Answers 7

3

The problem is that '118' is not an integer as far as the PHP parser is concerned, it's a string. It looks like an integer to us, of course, but it has slashes (') which make it "unconvertible".

Use str_replace for this:

intval(str_replace("'", '', $casestringid));
Sign up to request clarification or add additional context in comments.

2 Comments

typecasting (int) str_replace(...) is performanter then intval()
@donald123 That's true. In this case we are using intval with base 10, so it's better to use int casting. 0 + str_replace(...) would be even faster.
1

i think you have no other chance like this:

intval(str_replace("'",'',$casestringid));

2 Comments

someone said the exact same thing a minute before you. Thanks though it is right!
wait nvm you said it before
1

Replace the '':

intval(str_replace("'",'',$casestringid));

Comments

1

Try intval ($casestringid) + 0.

EDIT:

How about this, then:

filter_var ($casestringid, FILTER_SANITIZE_NUMBER_INT);

5 Comments

I dont think intval will digest quotes.
i think intval would result in zero.
You are mistaken, one of the many curiosities of PHP is the way it handles string-to-int conversion. It will basically take the largest substring of digits starting at index 0 it can find, and discard the rest, then convert those to int. For instance, saying intval('51 pigs') will return 51. EDIT: Never mind, I just noticed the ' at index 0.
I'm still getting the same result. $caseid is still 0
@Thanix using your example, it would be intval("'51 pigs'") which will return 0
0

You have to remove the single quotes and use intval().

<?php

$casestringid = "'118'";
$parseid = str_replace("'", "", $casestringid);
$caseid = intval($parseid);

echo $casestringid;

echo $caseid;

?>

Comments

0
$casestringid = "'118'";
$int = str_replace("'", "", $casestringid);
echo intval($int);

1 Comment

should still use intval() even after the string is replaced
0

If it is just an integer you are looking for, this could work. it will remove any non digit characters then return it as an int

function parseInt( $s ){
    return (int)(preg_replace( '~\D+~' , '' , $s ));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.