如何删除前导零?

时间:2013-02-23 00:22:07

标签: php mysql sql preg-replace

我在我使用的数据库中有一个名为last_count的列,其中填充了int。该数字具有大量0的基本格式,然后是实际数字(例如000001489)。我需要在数字之前删除零,然后将它们显示在一个表中,该表从查询'select last_count,query,job_id来自twitterinblack46.job,其中job_id in('。$ job_id_all。')由last_count desc命令;'

我正在调查preg_replace但是它也删除了其他零(例如,如果数字是00001480,它回显148而不是1480)。我正在使用php。

3 个答案:

答案 0 :(得分:1)

最简单的方法是将值转换为整数:

select cast(col as int)

这将显示没有前导零的数字。

如果您需要将值作为字符串,则可以将其转换为varchar:

select cast(cast col as int) as varchar(255))

答案 1 :(得分:1)

只需在php中将类型更改为int

// Your sql stuff
$stmt->bind_result($last_count);
while ($stmt->fetch()) {
    echo "<tr>";
    echo "<td>".intval($last_count)."</td>";  // Or use (int)$last_count
    echo "</tr>";
}

请参阅http://php.net/manual/en/function.intval.php

答案 2 :(得分:1)

这个怎么样?

preg_match("/[^0].*/",$number, $matches);
echo $matches[0];
相关问题