如何将带有数字和空格的字符串转换为int

发布于 2024-11-15 00:20:49 字数 170 浏览 2 评论 0原文

我有一个小问题。我正在尝试将“1 234”这样的字符串转换为数字:1234 我去不了那里。该字符串是从网站上抓取的。那里可能没有空间吗?因为我已经尝试过 str_replace 和 preg_split 等方法来获取空间,但什么也没有。另外 (int)$abc 仅采用第一个数字 (1)。 如果有人有想法,我会很高兴!谢谢你!

I have a small problem. I am tryng to convert a string like "1 234" to a number:1234
I cant't get there. The string is scraped fro a website. It is possible not to be a space there? Because I've tried methods like str_replace and preg_split for space and nothing. Also (int)$abc takes only the first digit(1).
If anyone has an ideea, I'd be greatefull! Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

酷到爆炸 2024-11-22 00:20:50
$str = "1 234";
$int = intval(str_replace(' ', '', $str)); //1234
$str = "1 234";
$int = intval(str_replace(' ', '', $str)); //1234
南烟 2024-11-22 00:20:50

我刚刚遇到了同样的问题,但是提供的答案并没有涵盖我遇到的所有不同情况...

所以我做了这个函数(感谢 Dan,这个想法突然出现在我的脑海中):

function customCastStringToNumber($stringContainingNumbers, $decimalSeparator = ".", $thousandsSeparator = " "){
    $numericValues = $matches = $result = array();
    $regExp = null;
    $decimalSeparator = preg_quote($decimalSeparator);
    $regExp = "/[^0-9$decimalSeparator]/";
    preg_match_all("/[0-9]([0-9$thousandsSeparator]*)[0-9]($decimalSeparator)?([0-9]*)/", $stringContainingNumbers, $matches);
    if(!empty($matches))
        $matches = $matches[0];
    
    foreach($matches as $match):
        $numericValues[] = (float)str_replace(",", ".", preg_replace($regExp, "", $match));
    endforeach;
    $result = $numericValues;
    if(count($numericValues) === 1)
        $result = $numericValues[0];

    return $result;
}

所以,基本上,该函数提取字符串中包含的所有数字,无论有多少文本,识别小数分隔符并将每个提取的数字作为浮点数返回。

您可以使用 $decimalSeparator 参数指定自己所在国家/地区使用哪种小数分隔符。

I've just came into the same issue, however the answer that was provided wasn't covering all the different cases I had...

So I made this function (the idea popped in my mind thanks to Dan) :

function customCastStringToNumber($stringContainingNumbers, $decimalSeparator = ".", $thousandsSeparator = " "){
    $numericValues = $matches = $result = array();
    $regExp = null;
    $decimalSeparator = preg_quote($decimalSeparator);
    $regExp = "/[^0-9$decimalSeparator]/";
    preg_match_all("/[0-9]([0-9$thousandsSeparator]*)[0-9]($decimalSeparator)?([0-9]*)/", $stringContainingNumbers, $matches);
    if(!empty($matches))
        $matches = $matches[0];
    
    foreach($matches as $match):
        $numericValues[] = (float)str_replace(",", ".", preg_replace($regExp, "", $match));
    endforeach;
    $result = $numericValues;
    if(count($numericValues) === 1)
        $result = $numericValues[0];

    return $result;
}

So, basically, this function extracts all the numbers contained inside of a string, no matter how many text there is, identifies the decimal separator and returns every extracted number as a float.

One can specify what decimal separator is used in one's country with the $decimalSeparator parameter.

笑着哭最痛 2024-11-22 00:20:50

使用此代码删除任何其他字符,例如 .,:"'\/, !@#$%^&*(), az >, AZ :

$string = "This string involves numbers like 12 3435 and 12.356 and other symbols like !@# then the output will be just an integer number!";

$output = intval(preg_replace('/[^0-9]/', '', $string));

var_dump($output);

Use this code for removing any other characters like .,:"'\/, !@#$%^&*(), a-z, A-Z :

$string = "This string involves numbers like 12 3435 and 12.356 and other symbols like !@# then the output will be just an integer number!";

$output = intval(preg_replace('/[^0-9]/', '', $string));

var_dump($output);
折戟 2024-11-22 00:20:49

我就是这样处理的...

<?php

$string = "Here! is some text, and numbers 12 345, and symbols !£$%^&";

$new_string = preg_replace("/[^0-9]/", "", $string);

echo $new_string // Returns 12345

?>

This is how I would handle it...

<?php

$string = "Here! is some text, and numbers 12 345, and symbols !£$%^&";

$new_string = preg_replace("/[^0-9]/", "", $string);

echo $new_string // Returns 12345

?>
放血 2024-11-22 00:20:49
intval(preg_replace('/[^0-9]/', '', $input))
intval(preg_replace('/[^0-9]/', '', $input))
感受沵的脚步 2024-11-22 00:20:49

抓取网站总是需要特定的代码,您知道如何接收输入 - 并且您编写使其可用所需的代码。

这就是为什么第一个答案仍然是str_replace。

$iInt = (int)str_replace(array(" ", ".", ","), "", $iInt);

Scraping websites always requires specific code, you know how you receive the input - and you write code that is required to make it usable.

That is why first answer is still str_replace.

$iInt = (int)str_replace(array(" ", ".", ","), "", $iInt);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文