0

I am trying to remove certain parts of my string using preg_match. I do not know if this is the best scenario.

I have the following string:

Uploaded Today 13:11, Size 117.09 MiB, ULed by demonoidt

I want to get only the 117.09 part of the string. I am using preg_match like this:

$res = preg_replace("/[^0-9]/", "", $string);

But this will return all the numbers found in the string, so I would have something like:

131111709

Any help would be greatly appreciated. Thank you and happy holidays.

3
  • The quickest way to end up with 117.09 would be $var = "117.09" This is obviously not what you meant, but i can think of any number of substring operations that would only work for this case. Could you clarify why you want that part? Commented Dec 28, 2016 at 12:56
  • do you want to extract 117.09 from the string? Commented Dec 28, 2016 at 13:13
  • It will never be the same, the information is stored in an array. that whole uploaded... and so on so it will not be just 117.09, that can change easily so that is why I went for the preg_replace at the first time, to find just the numbers in that string. Commented Dec 28, 2016 at 13:27

4 Answers 4

6

You should be using a matching approach rather than the replacing one.

Use

/([\d.]+)\s*MiB/

Or, if the Size word is always before the number, then use

/Size\s*([\d.]+)/

and grab Group 1 value. See the regex demo (or this demo).

Pattern details:

  • ([\d.]+) - Group 1 capturing 1 or more digits or .
  • \s* - 0+ whitespaces
  • MiB - a sequence of literal chars.

NOTE that you may make the number matching part more precise by replacing [\d.]+ with \d*\.?\d+.

PHP demo:

$re = '/([\d.]+)\s*MiB/';
$str = 'Uploaded Today 13:11, Size 117.09 MiB, ULed by demonoidt';
if (preg_match($re, $str, $match)) {
  echo $match[1]; // => 117.09
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can try this also -

$s = 'Uploaded Today 13:11, Size 117.09 MiB, ULed by demonoidt';

preg_match('/\d*\.\d*/', $s, $match);

var_dump($match[0]);

Output

string(6) "117.09"

\d* matches a digit (equal to [0-9])

Test

Comments

1

You can get it by following regex:

$re = '/(?<=Size )(\d.)+/';
$str = 'Uploaded Today 13:11, Size 117.09 MiB, ULed by demonoidt';
if (preg_match($re, $str, $match)) {
  echo $match[0]; // => 117.09
}

Comments

-1

@Cristian Badea if you want to extract particular part of string like 117.09 from your string so you can also get this by the php string function

try the below strpos, substr function to get your 117.09 string from the whole string

<?php
    $a = "Uploaded Today 13:11, Size 117.09 MiB, ULed by demonoidt";
    $extract = "117.09";
    if(strpos($a, $extract)){
        $b = substr($a, strpos($a, $extract), strlen($extract));
        echo $b;
    }
?>

In $b you will get your 117.09 part of string (y)

2 Comments

I need to be dynamic so I will never have 117.09 ... and the entire string is generated automatically, so I will have to get the size part.
How come this is an accepted answer? The 117.09 is a value to be extracted, it is not known beforehand. Its length is not known either, it can be 30, or 5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.