williamZanelli Posted August 19, 2009 Share Posted August 19, 2009 Just wanted to know what method I could use to compare strings, minus the apostrophe's, commas, full stops etc. I saw it a few days ago.. im prettyy sure it exists!! Thanks for your thoughts Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/ Share on other sites More sharing options...
mikesta707 Posted August 19, 2009 Share Posted August 19, 2009 What do you mean by "compare" are you trying to check if they are equal? check if they match a certain regular expression pattern? check if a string is inside another string? Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/#findComment-902058 Share on other sites More sharing options...
williamZanelli Posted August 19, 2009 Author Share Posted August 19, 2009 Well what I have is two strings eg. [string 1] The forums cool [string 2] The forum's cool So the only that differntiates these two is the apostophe, similary if I have 2 strings eg. [string 3] The forums cool: [string 4] The forums cool; Here the difference is the Semi/Colon - How would I compare 2 strings to see if they're equal, disregarding the colons, commas, apotrophes etc. Does that make sense? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/#findComment-902088 Share on other sites More sharing options...
mikesta707 Posted August 19, 2009 Share Posted August 19, 2009 ahh ok, well you could create a function to strip the string of whatever you want to disregard, and do a simple equal comparison function isEqual($string, $string2, $disregard){ $string = str_replace($disregard, "", $string); $string2 = str_replace($disregard, "", $string2); if ($string == $string2){ return true; } else { return false; } } $disregard = array(":", ";", "'", ",", "."); $answer = isEqual("Hello, my name's johnny;;", "Hello. my names johnny", $disregard); echo $answer; ?> Output: 1 Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/#findComment-902096 Share on other sites More sharing options...
Monadoxin Posted August 19, 2009 Share Posted August 19, 2009 <?php function compareStr($sString1, $sString2) { # Remove the characters you do not want $sNewString1 = ereg_replace("[^A-Za-z0-9]", "", $sString1); $sNewString2 = ereg_replace("[^A-Za-z0-9]", "", $sString2); # Compare and return True or False if($sNewString1 == $sNewString2) { return true; } else { return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/#findComment-902097 Share on other sites More sharing options...
kratsg Posted August 19, 2009 Share Posted August 19, 2009 <?php function compareStr($sString1, $sString2) { # Remove the characters you do not want $sNewString1 = ereg_replace("[^A-Za-z0-9]", "", $sString1); $sNewString2 = ereg_replace("[^A-Za-z0-9]", "", $sString2); # Compare and return True or False if(strcasecmp($sNewString1,$sNewString2) == 0) { return true; } else { return false; } ?> http://us3.php.net/manual/en/function.strcasecmp.php Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/#findComment-902115 Share on other sites More sharing options...
nrg_alpha Posted August 19, 2009 Share Posted August 19, 2009 ereg should be dropped like a bad habit, as it is part of POSIX (Portable Operating System Interface uniX) is already considered depreciated as of current stable release PHP 5.3, and will no longer be included within the core by default as of version 6. Learn pcre (PCRE - Perl Compatible Regular Expressions) instead. function compareStr($sString1, $sString2) { $charStrip = ';:\'"'; // list of characters to remove $sNewString1 = preg_replace('#[' . $charStrip . ']#', '', $sString1); $sNewString2 = preg_replace('#[' . $charStrip . ']#', '', $sString2); return(strcasecmp($sNewString1,$sNewString2) == 0)? true : false; } $str01 = 'The forums cool:'; $str02 = 'The forum\'s cool'; echo compareStr($str01, $str02); Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/#findComment-902143 Share on other sites More sharing options...
nrg_alpha Posted August 19, 2009 Share Posted August 19, 2009 Alternatively, we could simply skip the $sNewString1 and $sNewString2 variable creation process altogether and simply stuff it all into a ternary operator: function compareStr($sString1, $sString2) { $charStrip = ';:\'"'; // list of characters to remove return (strcasecmp(preg_replace('#[' . $charStrip . ']#', '', $sString1), preg_replace('#[' . $charStrip . ']#', '', $sString2)) == 0)? true : false; } Quote Link to comment https://forums.phpfreaks.com/topic/171036-method-to-compare-strings/#findComment-902149 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.