profitweaver Posted October 5, 2008 Share Posted October 5, 2008 Hi, I am a novice at this. I am trying to find an instance of a string within another string. I have found several functions that do this. I have tested the functions and they seem to work ok, so I know the syntax is correct. However, I am passing a variable containing one string to this function in another file. The string is being passed ok, but the function will not correctly tell me that one string contains another when I know that it does. For example. <?php $pos = stristr("<?=$cat?>", "<?=$choosecat?>"); if ($pos === false) echo "The string '$choosecat' was not found in the string '$cat'"; else { echo "The string '$choosecat' was found in the string '$cat'"; echo " and exists at position $pos"; } ?> always returns false. Can anyone give me a clue why this isn't working please? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/127095-solved-string-matching/ Share on other sites More sharing options...
ibechane Posted October 5, 2008 Share Posted October 5, 2008 Of course that always returns false. It's like asking, "Is the word 'judge' inside the word 'judgment'" and the answer is no, because there is no 'e' after the the 'g' in judgment. However, if you ask "Is the 'ball' inside the word 'ballpark', then the answer would be yes, because the word 'ball' exists in its entirety in 'ballpark'. Quote Link to comment https://forums.phpfreaks.com/topic/127095-solved-string-matching/#findComment-657417 Share on other sites More sharing options...
sKunKbad Posted October 5, 2008 Share Posted October 5, 2008 Try: $pos = stristr($cat,$choosecat); if ($pos == false) Quote Link to comment https://forums.phpfreaks.com/topic/127095-solved-string-matching/#findComment-657419 Share on other sites More sharing options...
ibechane Posted October 5, 2008 Share Posted October 5, 2008 nevermind my post. I didn't read your arguments as variables. stristr matches the value inside the variables, so you don't need to print them out. sKunKbad's method should work for you. Quote Link to comment https://forums.phpfreaks.com/topic/127095-solved-string-matching/#findComment-657420 Share on other sites More sharing options...
profitweaver Posted October 6, 2008 Author Share Posted October 6, 2008 Try: $pos = stristr($cat,$choosecat); if ($pos == false) Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/127095-solved-string-matching/#findComment-657982 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.