جلال قرومي
جلال قرومي

Reputation: 31

How to delete only float numbers in my string text

This is my variable, so i want that php removes all float numbers in my string and Print this result on the browser:

   <?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?
   ?>

Upvotes: 1

Views: 43

Answers (2)

Code4R7
Code4R7

Reputation: 2958

A way to get the job done:

$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

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94672

<?php
$var = "100 1-testB/10000 20000.100 200 2-testB/2/20000 20000.200 300 3-testB/30000 30000.3000";
$output = preg_replace("/\s*\b\d+(?:\.\d+)?\b\s*/", " ", $var);
$output = trim(preg_replace('/[\s\-\/]+/mu', ' ', $output));
echo $output;

RESULT

testB testB testB

Upvotes: 0

Related Questions