Page 1 of 1
How to check if is a string is similar to other?
Posted: Fri May 22, 2009 3:07 pm
by zenon
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!
Re: How to check if is a string is similar to other?
Posted: Fri May 22, 2009 3:23 pm
by requinix
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)
Re: How to check if is a string is similar to other?
Posted: Fri May 22, 2009 3:25 pm
by anand
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%';")
This might help you.
Re: How to check if is a string is similar to other?
Posted: Fri May 22, 2009 3:33 pm
by zenon
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
Code: Select all
if ($str isSimilarTo $stringDataBase) {
doSomething;
}
I'm really sorry if i confused you!
Re: How to check if is a string is similar to other?
Posted: Fri May 22, 2009 3:42 pm
by anand
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 like
Code: Select all
if ($str isSimilarTo $stringDataBase) {
doSomething;
}
I'm really sorry if i confused you!
How about using eregi ?
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?
Posted: Fri May 22, 2009 4:02 pm
by zenon
anand wrote: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 like
Code: Select all
if ($str isSimilarTo $stringDataBase) {
doSomething;
}
I'm really sorry if i confused you!
How about using eregi ?
its possible if $stringDataBase = $str + $additional message.
Code: Select all
if(eregi($str, $stringDataBase)) {
doSomething;
}
Thank you very much!!
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?
Posted: Fri May 22, 2009 4:53 pm
by requinix
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';
Re: How to check if is a string is similar to other?
Posted: Fri May 22, 2009 4:59 pm
by zenon
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';
Thank you tasairis very much!
I'm speechless! Really thank you!
Thank all guys!
I wouldn't do anything without your precious help!