search in varaible/string function

Discussion in 'PHP' started by mdrobiul, May 30, 2009.

  1. #1
    Hello , can please tell me any name of built in function that returns true/or false on with given variable and findable string .. like strstr or substr etc. but they return a output but i want boolean value on return..
     
    mdrobiul, May 30, 2009 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just typecast the result as a bool:
    echo (bool) stristr('mystring', 'str');
    Code (markup):
    PS "Beginner PHP Expert" :D Dude, wtf... :)
     
    xlcho, May 30, 2009 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    strpos will return a boolean false for not found, or an integer indicating the position.

    if(strpos('...', $str) === false)
    Code (markup):
     
    joebert, May 30, 2009 IP
  4. xcrox

    xcrox Banned

    Messages:
    232
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    xcrox, May 30, 2009 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    $string = 'Hello world';
    $needle = 'world';
    $find = strpos($string, $needle) > 0;
     
    php-lover, May 30, 2009 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    No.

    You must check for a boolean false using the strict-equality operator (===) in order to make it work with strpos. Unless you want it to miss it being the first word in the string.
     
    joebert, May 31, 2009 IP