175

What would be the most efficient way to check whether a string contains a "." or not?

I know you can do this in many different ways like with regular expressions or loop through the string to see if it contains a dot (".").

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
ealeon
  • 12,074
  • 24
  • 92
  • 173

3 Answers3

496

PHP 8 or newer:

Use the str_contains function.

if (str_contains($str, "."))
{
    echo 'Found it';
}

else
{
    echo 'Not found.';
}

PHP 7 or older:

if (strpos($str, '.') !== FALSE)
{
    echo 'Found it';
}

else
{
    echo 'Not found.';
}

Note that you need to use the !== operator. If you use != or <> and the '.' is found at position 0, the comparison will evaluate to true because 0 is loosely equal to false.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
akatakritos
  • 9,836
  • 1
  • 23
  • 29
  • 94
    +1 for "This will cause you to point a production website at a development database over the weekend, causing no end of joy when you return monday." – Jordan Arsenault Jun 25 '13 at 23:07
  • 4
    Replace strpos with stripos for case insensitive searches. – Tegan Snyder Oct 12 '15 at 15:47
  • @JordanArseno What makes you say that? Nested code? You suggest he encloses it into a function? – inrob Nov 24 '15 at 20:30
  • 1
    `if (count(explode('.', $string)) != 2) { //string don't have dot }` – Tahir Yasin Nov 11 '16 at 10:51
  • 1
    @TahirYasin This is incorrect - what if there are multiple dots... This will cause you to point a _development_ website at a _production_ database, erasing it and causing no end of joy when you return Monday – Hack5 Jun 14 '17 at 18:58
  • @Penn The question is to find if a given string contains dot, and the code I posted will work perfectly fine for that purpose (either there is one dot or multiple) but if you are using it in some other scenario where you need to take care of other parameters as well then it's a logical fault and you can not blame the code. – Tahir Yasin Jun 15 '17 at 05:37
  • @tahir-yasin It will fail for multiple periods as the count will be more than 2. But why create all of this overhead in the first place when strpos is faster, memory efficient, more readable, and exists specifically for this purpose. – hndcrftd Jan 10 '20 at 17:42
66

You can use these string functions,

strstr — Find the first occurrence of a string

stristr — Case-insensitive strstr()

strrchr — Find the last occurrence of a character in a string

strpos — Find the position of the first occurrence of a substring in a string

strpbrk — Search a string for any of a set of characters

If that doesn't help then you should use preg regular expression

preg_match — Perform a regular expression match

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
7

You can use stristr() or strpos(). Both return false if nothing is found.

Jefferson
  • 794
  • 10
  • 24
Sylverdrag
  • 8,898
  • 5
  • 37
  • 54
  • 5
    It's very important to note that you cannot check for "falsy", it has to be false. Use the identical operator (=== or !==), as strpos and stristr return 0 as the 0th position, but if you use an "equal to" (== or !=) it believe that it is false. – Mattisdada May 16 '14 at 04:36
  • @Mattisdada Very good point, thanks for pointing that out. – Sylverdrag May 16 '14 at 15:02