How to check if is a string is similar to other?
Moderator: General Moderators
How to check if is a string is similar to other?
Good evening.
I need some help.
I have a string, that i retrieve from a database and it's like "1: Hello (something)".
Is there a way to check if "1: Hello" is similar to "1: Hello (something)" ?
Because i have different values in the database, like "1: Hello (somethingOne)", "1: Hello (somethingTwo)", "1: Hello (somethingThree)", ..., "2: Bye (something)", "3: Hurray (something)".
I have an if loop and if the string from database contains "1: Hello" , it will do something.
I hope i didn't confuse you!
Any help accepted!
Thank you!
I need some help.
I have a string, that i retrieve from a database and it's like "1: Hello (something)".
Is there a way to check if "1: Hello" is similar to "1: Hello (something)" ?
Because i have different values in the database, like "1: Hello (somethingOne)", "1: Hello (somethingTwo)", "1: Hello (somethingThree)", ..., "2: Bye (something)", "3: Hurray (something)".
I have an if loop and if the string from database contains "1: Hello" , it will do something.
I hope i didn't confuse you!
Any help accepted!
Thank you!
Re: How to check if is a string is similar to other?
Depends how you define "similar".
Starts with? [url=htp://php.net/manual/en/function.strncmp.php]strncmp[/url]
<X characters missing, added, or changed? levenshtein (take advantage of giving weights per operation)
Starts with? [url=htp://php.net/manual/en/function.strncmp.php]strncmp[/url]
<X characters missing, added, or changed? levenshtein (take advantage of giving weights per operation)
Re: How to check if is a string is similar to other?
zenon wrote:Good evening.
I need some help.
I have a string, that i retrieve from a database and it's like "1: Hello (something)".
Is there a way to check if "1: Hello" is similar to "1: Hello (something)" ?
Because i have different values in the database, like "1: Hello (somethingOne)", "1: Hello (somethingTwo)", "1: Hello (somethingThree)", ..., "2: Bye (something)", "3: Hurray (something)".
I have an if loop and if the string from database contains "1: Hello" , it will do something.
I hope i didn't confuse you!
Any help accepted!
Thank you!
Code: Select all
mysql_query("SELECT * FROM table WHERE message LIKE '%Hello%';")Re: How to check if is a string is similar to other?
Thank you for your quick answers!
anand thank you but it's not that i'm looking for.
tasairis, using the word "similar", i mean both strings begin the same, but the string from database has some more characters.
Because data in database change often, i want to write something like
I'm really sorry if i confused you!
anand thank you but it's not that i'm looking for.
tasairis, using the word "similar", i mean both strings begin the same, but the string from database has some more characters.
Because data in database change often, i want to write something like
Code: Select all
if ($str isSimilarTo $stringDataBase) {
doSomething;
}
Last edited by Benjamin on Fri May 22, 2009 3:35 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Reason: Changed code type from text to php.
Re: How to check if is a string is similar to other?
How about using eregi ?zenon wrote:Thank you for your quick answers!
anand thank you but it's not that i'm looking for.
tasairis, using the word "similar", i mean both strings begin the same, but the string from database has some more characters.
Because data in database change often, i want to write something likeI'm really sorry if i confused you!Code: Select all
if ($str isSimilarTo $stringDataBase) { doSomething; }
its possible if $stringDataBase = $str + $additional message.
Code: Select all
if(eregi($str, $stringDataBase)) {
doSomething;
}Re: How to check if is a string is similar to other?
Thank you very much!!anand wrote:How about using eregi ?zenon wrote:Thank you for your quick answers!
anand thank you but it's not that i'm looking for.
tasairis, using the word "similar", i mean both strings begin the same, but the string from database has some more characters.
Because data in database change often, i want to write something likeI'm really sorry if i confused you!Code: Select all
if ($str isSimilarTo $stringDataBase) { doSomething; }
its possible if $stringDataBase = $str + $additional message.
Code: Select all
if(eregi($str, $stringDataBase)) { doSomething; }
This function solved my problem!
Have a peaceful night and thank you once again! I really appreciate your precious help!
Re: How to check if is a string is similar to other?
ereg is (wasteful, and) will look if one string contains the other.
No offense to anand but strncmp is really the best for this.
No offense to anand but strncmp is really the best for this.
Code: Select all
function startswith($string, $substring) {
return strncmp($string, $substring, strlen($substring)) == 0;
}
$string = "1. Hello (something)";
$substring = "1. Hello";
echo startswith($string, $substring)
? '$string starts with $substring'
: '$string does not start with $substring';Re: How to check if is a string is similar to other?
Thank you tasairis very much!tasairis wrote:ereg is (wasteful, and) will look if one string contains the other.
No offense to anand but strncmp is really the best for this.Code: Select all
function startswith($string, $substring) { return strncmp($string, $substring, strlen($substring)) == 0; } $string = "1. Hello (something)"; $substring = "1. Hello"; echo startswith($string, $substring) ? '$string starts with $substring' : '$string does not start with $substring';
I'm speechless! Really thank you!
Thank all guys!
I wouldn't do anything without your precious help!