glenelkins Posted September 2, 2009 Share Posted September 2, 2009 Hi Just wondering if anyone can see why this if statement doesnt execute the die() function: $var1 = 'something.something.co.uk'; $var2 = '.co.uk'; if ( strstr ( $var1, $var2 ) ) { die ( 'OK' ); } Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/ Share on other sites More sharing options...
glenelkins Posted September 2, 2009 Author Share Posted September 2, 2009 this is stange! if the $var2 is pulled from the database strstr doesnt work...if i set it manually it does work..wtf! Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/#findComment-910792 Share on other sites More sharing options...
ignace Posted September 2, 2009 Share Posted September 2, 2009 From the manual: Returns part of haystack string from the first occurrence of needle to the end of haystack. Thus strstr returns an empty string as there isn't anything left after .co.uk therefor use type comparison: if (false !== strstr($var1, $var2)) { Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/#findComment-910795 Share on other sites More sharing options...
ignace Posted September 2, 2009 Share Posted September 2, 2009 this is stange! if the $var2 is pulled from the database strstr doesnt work...if i set it manually it does work..wtf! Can you post this script, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/#findComment-910796 Share on other sites More sharing options...
glenelkins Posted September 2, 2009 Author Share Posted September 2, 2009 i will post some example code. It makes no sense to why the strstr is not picking up the .co.uk from the first array element. The * is removed so the needle becomes .co.uk $array = array ( '*.co.uk', '*.com' ); // THE ARRAY IS GENERATED FROM DATABASE SERIALIZED DATABASE VALUES. using unserialize() to create the array // if i create the array manually then it works ok $hostname = gethostbyaddr ( '111.111.111.111' ); // for example. server.domain.co.uk foreach ( $array as $item ) { // remove the * // will leave .co.uk on the first element $item = ltrim ( $item, '*' ); if ( strstr ( $hostname, $item ) ) { die ( 'OK' ); } } Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/#findComment-910802 Share on other sites More sharing options...
glenelkins Posted September 2, 2009 Author Share Posted September 2, 2009 here ill add a bit more for the example. lets build the array from an imaginery database table $result = mysql_query ( "SELECT `data` FROM `test`" ); list ( $data ) = mysql_fetch_array ( $result ); $array = unserialize ( $data ); // array ( '*.co.uk' ) The strstr doesnt work with this however it will work if i do: $array = array ( '*.co.uk' ); WTF WTF WTF Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/#findComment-910807 Share on other sites More sharing options...
glenelkins Posted September 2, 2009 Author Share Posted September 2, 2009 the problem seems to occur when the array has more than 1 item Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/#findComment-910815 Share on other sites More sharing options...
glenelkins Posted September 2, 2009 Author Share Posted September 2, 2009 solved..it was a bloody \n causing the issue Quote Link to comment https://forums.phpfreaks.com/topic/172803-solved-strstr/#findComment-910821 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.