Jump to content

Method to compare strings


williamZanelli

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

<?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;
	}
?>

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.