开发者

PHP string to integer question

Q开发者_运维知识库uick question:

I have strings in the form of '121', '9998', etc. They are literally numbers surrounded by the single quotes.

How can I remove these quotes and cast them as integers? I'm passing to another program that needs them to be integers.

Thanks.


Use trim() and intval():

$n = intval(trim($str, "'"));


There are a few ways to do this, but the most common are:

$int = intval($string);

Or, my preference:

$int = (int)$string;

Since $string has a literal single quote, you can trim() it first by taking advantage of its second parameter.

$int = (int)trim($string, "'");

Remember that PHP is a weak typed, dynamic language.


$int = (int)trim("'121'", "'");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜