-2

I am trying to check if a string contains .2.

The following code works if I try to check if it contains a dot:

strpos($string, '.') !== false

But it doesn't work when use the following code:

strpos($string, '.2') !== false

nor

strpos($string, '\.2') !== false

Anyone an idea how to write it correctly? Thanks!

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Nicolas123
  • 47
  • 1
  • 1
  • 8

1 Answers1

1

Ok, here is a working solution:

$string = "this text contains the specified format: .233 and some nonsense.";
preg_match('/^.*(\.\d+).*$/m', $string, $matches);

var_dump($matches);

//returns
array(2) {
  [0]=>
  string(64) "this text contains the specified format: .233 and some nonsense."
  [1]=>
  string(4) ".233"
}

see the example at: http://regex101.com/r/eF0cU7/1

Raphael Müller
  • 2,180
  • 2
  • 15
  • 20