tcloud Posted October 9, 2010 Share Posted October 9, 2010 I need to know if a string exists within a string. I've tried using STRIPOS() but it returns zero or false if what I'm looking for is the first part of the string. I don't care what function I use, but I need to get a "true" when the needle string is present anywhere in the haystack string, even at location zero. Is there some way to set up the STRIPOS() statement? I've tried: if ((stripos($title, 'needle') > 0 ) if ((stripos($title, 'needle') = true ) and even if ((stripos($title, 'needle') >= 0 ) which returns true for everything, even when the needle string is NOT present. Any help appreciated. thanks, Tom Quote Link to comment https://forums.phpfreaks.com/topic/215508-help-finding-string-in-a-string-eg-stripos/ Share on other sites More sharing options...
BlueSkyIS Posted October 9, 2010 Share Posted October 9, 2010 maybe try stristr: stristr (PHP 3 >= 3.0.6, PHP 4, PHP 5) stristr -- Case-insensitive strstr() Description string stristr ( string haystack, string needle ) Returns all of haystack from the first occurrence of needle to the end. needle and haystack are examined in a case-insensitive manner. If needle is not found, returns FALSE. If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. Example 1. stristr() example <?php $email = 'USER@EXAMPLE.com'; echo stristr($email, 'e'); // outputs ER@EXAMPLE.com ?> Quote Link to comment https://forums.phpfreaks.com/topic/215508-help-finding-string-in-a-string-eg-stripos/#findComment-1120605 Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 try: if ((stripos($title, 'needle') !== false ) Quote Link to comment https://forums.phpfreaks.com/topic/215508-help-finding-string-in-a-string-eg-stripos/#findComment-1120607 Share on other sites More sharing options...
tcloud Posted October 9, 2010 Author Share Posted October 9, 2010 maybe try stristr: stristr (PHP 3 >= 3.0.6, PHP 4, PHP 5) stristr -- Case-insensitive strstr() Description string stristr ( string haystack, string needle ) Returns all of haystack from the first occurrence of needle to the end. needle and haystack are examined in a case-insensitive manner. If needle is not found, returns FALSE. If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. Example 1. stristr() example <?php $email = 'USER@EXAMPLE.com'; echo stristr($email, 'e');// outputs ER@EXAMPLE.com?> thanks, that worked: if (stristr($title, 'needle') == true) works properly even if "needle" is at the beginning of the string. Tom Quote Link to comment https://forums.phpfreaks.com/topic/215508-help-finding-string-in-a-string-eg-stripos/#findComment-1120618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.