如何摆脱PHP中的字符串文字中的额外元素?

时间:2010-02-05 20:54:49

标签: php string

我的号码像9843324+,现在我想在最后摆脱+并且只有9843324,所以我该如何在php中执行此操作?

现在我正在做$customer_id = explode('+',$o_household->getInternalId); $o_household->getInternalId还给我9843324+,但我想要9843324,我怎么能实现这个目标?

感谢。

6 个答案:

答案 0 :(得分:5)

如果您只想弹出+,请使用substr

$customer_id = substr($o_household->getInternalId, 0, -1);

rtrim

$customer_id = rtrim($o_household->getInternalId, "+");

答案 1 :(得分:2)

您可以使用正则表达式删除任何非数字

的内容

$newstr = preg_replace("/[^0-9]+/","",$str);

答案 2 :(得分:1)

$customer_id = rtrim ( $o_household->getInternalId, '+' );

rtrim function reference

rtrim从字符串末尾删除第二个参数(仅在这种情况下为+)中的字符。

如果字符串末尾没有+,这不会像substr那样搞乱你的价值。

这个解决方案显然比preg_replace更具可读性和快速性。

答案 3 :(得分:0)

你可以使用intval($ o_household-> getInternalId)

答案 4 :(得分:0)

你需要使用爆炸吗?我会像Andy建议的那样使用substrstr_replace。要么适用于您提供的示例。

答案 5 :(得分:0)

如果可能,您可以使用intval获取变量的整数值:

echo intval($str);

请注意,intval将在失败时返回0。

相关问题