php - 如何仅删除字符串文本中的浮点数
问题描述
这是我的变量,所以我希望 php 删除我的字符串中的所有浮点数并在浏览器上打印这个结果:
<?php
//1- this my variable
$var = "100 1-testB/10000 20000.100 200 2-testB/2/20000 20000.200 300 3-testB/30000 30000.3000";
//2- I want to delete only the float numbers in my string and get This Result:
//Result I want: 100 1-testB/10000 200 2-testB/2/20000 300 3-testB/30000
//3- i wrote this code for to do that:
$output = trim(preg_replace("/\s*\b\d+(?:\.\d+)?\b\s*/", " ", $var));
echo $output;
//4- but the result is that: -testB/ -testB/ / -testB/
//#any help please?
?>
解决方案
完成工作的方法:
$var = "100 1-testB/10000 20000.100 200 2-testB/2/20000 20000.200 300 3-testB/30000 30000.3000";
$output = explode(' ', $var);
foreach ($output as $k => $v) {
if (FALSE !== strpos($v, '.')) {unset($output[$k]);}
}
$output = implode(' ', $output);
echo $output; // 100 1-testB/10000 200 2-testB/2/20000 300 3-testB/30000
推荐阅读
- reactjs - 如何在 ant design 或 material UI 中使用 react-hook-form
- database - 使用 UNIX 建立与 DB2 的连接
- unity3d - 脚本创建的 GameObject(Prefab 对象)在 Unity3d 中不出现在 Game View 中但出现在 Scene View 中
- time-complexity - 大写 N 与小 n 的时间复杂度
- c++ - SFML 从文件加载完成并出现错误代码
- angular - 指令中的 NgControl:没有 NgControl 的提供者
- angular - 无法设置未定义的属性“分页器” - 单元测试
- regex - 查找 Dropbox 路径在某些计算机上不起作用
- cron - 命令在终端中运行良好,但在作为 cron 作业运行时不能
- batch-file - 批处理循环以使用时间戳重命名并将文件从一个文件夹移动到另一个文件夹