如何将带有数字和空格的字符串转换为 int [英] How to convert a string with numbers and spaces into an int

查看:79
本文介绍了如何将带有数字和空格的字符串转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题.我正在尝试将像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!

推荐答案

这就是我的处理方式...

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

?>

这篇关于如何将带有数字和空格的字符串转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆