How to check if is a string is similar to other?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

How to check if is a string is similar to other?

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to check if is a string is similar to other?

Post 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)
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: How to check if is a string is similar to other?

Post 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.
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: How to check if is a string is similar to other?

Post 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!
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.
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: How to check if is a string is similar to other?

Post 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;
 
}
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: How to check if is a string is similar to other?

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to check if is a string is similar to other?

Post 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';
zenon
Forum Commoner
Posts: 42
Joined: Fri Jan 23, 2009 1:41 pm

Re: How to check if is a string is similar to other?

Post 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!
Post Reply