The strpos of function determines whether a string contains a string

  • 2021-11-13 01:05:24
  • OfStack

Method of judging whether a string contains a certain string by strpos () function of php


 A method of judging whether a string contains a string 
if(strpos('www.idc-gz.com','idc-gz') !== false){

 
          echo ' Include ';

 
        }else{

 
        echo ' Does not contain ';

 
        }

PHP strpos () Function

The strpos () function returns the first occurrence of a string in another string. If the string is not found, false is returned.
Grammar

Parameter description of strpos (string, find, start) string required. Specifies the string to be searched for. find Required. Specifies the characters to find. start is optional. Specify where to start the search.

Note: This function is case sensitive. For case-insensitive searches, use the stripos () function.

Edit this example


    <?php
    echo strpos(www.idc-gz.com,"idc");
    ?> 

Output: 4

A method of judging whether a string contains a string


if(strpos('www.idc-gz.com','idc-gz') !== false){


    echo ' Include ';


    }else{


    echo ' Does not contain ';


    }

Many people use the following judgment methods, which are wrong:


if(strpos('www.idc-gz.com','idc-gz') ){


    echo ' Include ';


    }else{


    echo ' Does not contain ';


    }

The above can also get the correct result, but the method is wrong. If if (strpos ('idc-gz. com', 'idc-gz')), you can't get the correct result, because the position starts from 0, and the first position is found, that is, 0, and 0 in php is not true. The above judgment will not hold, which requires 10 points of attention!


Related articles: