首页 > 解决方案 > 如何仅删除字符串文本中的浮点数

问题描述

这是我的变量,所以我希望 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?
   ?>

标签: php

解决方案


完成工作的方法:

$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

推荐阅读