1

There is a string variable containing number data with dots , say $x = "OP/1.1.2/DIR"; . The position of the number data may change at any circumstance by user desire by modifying it inside the application , and the slash bar may be changed by any other character ; but the dotted number data is mandatory. So how to extract the dotted number data , here 1.1.2, from the string ?

pheromix
  • 18,213
  • 29
  • 88
  • 158

1 Answers1

5

Use a regular expression:

(\d+(?:\.\d+)*)

Breakdown:

  • \d+ look for one or more digits
  • \. a literal decimal . character
  • \d+ followed by one or more digits again
  • (...)* this means match 0 or more occurrences of this pattern
  • (?:...) this tells the engine not to create a backreference for this group (basically, we don't use the reference, so it's pointless to have one)

You haven't given much information about the data, so I've made the following assumptions:

  • The data will always contain at least one number
  • The data may contain only a number without a dot
  • The data may contain multi-digit numbers
  • The numbers themselves may contain any number of dot/digit pairs

If any of these assumptions are incorrect, you'll have to modify the regular expression.

Example usage:

$x = "OP/1.1.2/DIR";

if (!preg_match('/(\d+(\.\d+)*)/', $x, $matches)) {
    // Could not find a matching number in the data - handle this appropriately
} else {
    var_dump($matches[1]); // string(5) "1.1.2"
}
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • 1
    `'/(\d+(?:\.\d+)*)/'` consumes less memory. – Florent Jul 12 '12 at 13:28
  • @Florent: Thank you for catching that. I'll add a bit about it in the breakdown. – FtDRbwLXw6 Jul 12 '12 at 13:32
  • Is it always at $matches[1] to get the data ? or can I get it at $matches[0] ? – pheromix Jul 12 '12 at 13:34
  • @pheromix: `$matches[0]` will always be the entire matching pattern. Because in this case the entire pattern should always be the entire number (because we aren't matching surrounding context), you could use the `0` offset. I'm in the habit of always using the `1` offset, because most expressions I write include context. In this example, if the `/` characters in the data weren't variable, we would have stuck those in the expression as well, but then `0` would have returned them as well as the number. I would still use `1`, though, in case you ever modify the pattern. – FtDRbwLXw6 Jul 12 '12 at 13:37