非常简单的代码。为什么最后我会变得空虚——我不明白。除数字和点外,所有内容都从文本中删除。结果,文本变为 24.5.。接下来,应用一个函数来检查最后一个字符,如果它是一个点,则将其删除。接下来打电话给自己。如果不是点,则返回数据。事实上,输出是空的。我的门框是什么?
$text = "24.5 кв.м.";
$text = preg_replace('~[^.0-9]+~', "", $text);
function removeDotintheend($text)
{
$last_symbol = substr($text, strlen($text)-1, 1);
if ($last_symbol == "."){
$text = substr($text, 0, strlen($text)-1);
removeDotintheend($text);
}
else
{
return $text;
}
}
$text = removeDotintheend($text);
echo $text;
错误出现在 removeDotintheend($text); 你正在执行一个函数,但不接受它的结果。并且有必要删除 else{}
检查http://sandbox.onlinephpfunctions.com/code/2516794d5c7257c91437e3be9517a16066fbf283
removeDotintheend
应该删除该函数,替换 libraryrtrim
。以稍微复杂一点的正则表达式为代价,这个函数也可以去掉。
从输入的数据和想要得到的结果来看,使用函数比较容易:
结果我们得到 24.5