Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if '.'[dot] is present in string or not ? strstr is not working in php

Tags:

php

strstr

I am trying to see if . [dot] is present in string or not.

I have tried strstr but it returns false.

here is my code :-

<?php
$str = strpos("true.story.bro", '.');
if($str === true)
{
        echo "OK";
} else {
        echo "No";
}
?>

I want to see if '.' is present in string or not, I can do with explode, but i want to do in 1 line, how could i do that ?

Thanks

like image 713
UserHex Avatar asked Feb 03 '13 19:02

UserHex


People also ask

How do you check if a dot is present in a string?

The easiest way is to check with a . contains() statement. Note: . contains() works only for strings.

How do you check if a substring is present in a string in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do you check if string does not contain substring?

To check if a string doesn't include a substring, call to the indexOf() method on the string, passing it the substring as a parameter. If the indexOf method returns -1 , then the substring is not contained in the string.

How check string is value or not in PHP?

The is_string() function checks whether a variable is of type string or not. This function returns true (1) if the variable is of type string, otherwise it returns false/nothing.


1 Answers

You may use strpos directly.

if (strpos($mystring, ".") !== false) {
    //...
}

Hope this help :)

like image 176
Frederik.L Avatar answered Oct 23 '22 13:10

Frederik.L