I am doing a quick duplicate check on a form. When comparing two strings, I was trying something like this:
if (stripos($_SESSION['website'], $f['website']))
I am getting this error:
Fatal error: Call to undefined function: stripos() in...
I do not want it to be an exact match, basically if the $_SESSION['website'] is http://www.google.com, I’d want stripos to return true if $f['website] is http://www.goog.com
Am I doing something wrong, or is there a better way to do this?
edit: I was doing some testing, and noticed if my $_SESSION['website'] variable contains www and .com. As does my $f['website] variable. shouldn’t strpos return that as true?
stripos(), as per the manual only exists in the core of PHP 5 or later. If stripos doesn’t exist, that would suggest you’re using PHP 4, which is wildly outdated. I suggest you upgrade. If for some insane reason you really can’t, you can always force the two strings to lowercase before calling strpos, which would have the same effect:
That said, there’s something more to your code: you’re checking
if( stripos( $foo, $bar ) )which would return 0 if the string$foostarts with the string$bar. You should check withif( stripos( $foo, $bar ) !== false )instead.On another note: I don’t think stripos will help you. You mentioned
www.google.comandwww.goog.com, which are two totally different things. The latter is not a substring of the former, and stripos checks for the starting position of a substring, it doesn’t do “loose comparison”.If you want “like” and you have the terms in an array, you might want to check out the similar_text() function which will give you an indication of how similar two different strings are. As an example:
The above code would set
$similaritytofloat(92.307692307692), which is the similarity of the two strings in percentages. You can decide the threshold for the similarity yourself, naturally.