|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Newbie - is there a function similar to the sql 'like' comparison operator?
I would like to be able to compare 2 strings: If $this ---*like or similar to*--- $that That type of thing... I know of this page: http://us3.php.net/manual/sl/languag...comparison.php But I don't see something 'like' or 'similar' to something else -- Thanks - RevDave Cool @ hosting4days . com [db-lists] |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Hi RevDave
Theres the similar_text() function which calculates the procentage of te similarity between two strings. If your looking for a check to see if a string has some specified text then use the strpos() function and then the identical comparison operator to check for true and false Kalle Sommer Nielsen DR - www.dr.dk ----- Original Message ----- From: "revDAVE" <Cool@hosting4days.com> To: "PHP General" <php-general@lists.php.net> Sent: Thursday, May 08, 2008 5:20 PM Subject: [php] Newbie - is there a function similar to the sql 'like' comparison operator? > Newbie - is there a function similar to the sql 'like' comparison > operator? > > I would like to be able to compare 2 strings: > > If $this ---*like or similar to*--- $that > > That type of thing... > > > I know of this page: > > http://us3.php.net/manual/sl/language.operators.comparison.php > > > But I don't see something 'like' or 'similar' to something else > > > -- > Thanks - RevDave > Cool @ hosting4days . com > [db-lists] > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > -- > No virus found in this incoming message. > Checked by AVG. > Version: 7.5.524 / Virus Database: 269.23.10/1421 - Release Date: > 07-05-2008 17:23 > > |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
revDAVE wrote:
> Newbie - is there a function similar to the sql 'like' comparison operator? > > I would like to be able to compare 2 strings: > > If $this ---*like or similar to*--- $that > > That type of thing... > > > I know of this page: > > http://us3.php.net/manual/sl/languag...comparison.php > > > But I don't see something 'like' or 'similar' to something else > > > -- > Thanks - RevDave > Cool @ hosting4days . com > [db-lists] > > > Probably the most similar that allows some wildcards (pattern matching) is ereg(). You have more power in that you can control what must match and how much. In MySQL 'a' = ' a ' is true but 'a' LIKE ' a ' is false. So you need to use the wildcard % to match anything ' xxaxx ' LIKE '%a%' is true or _ to match one character ' a ' LIKE '_a_' is true. The problem with similar_text() is that it gives you a percentage of similarity which may be misleading unless you also figure in the return of how many chars match. For example similar_text('CAT', 'CA', &$p) $p would be 66.6%, is that like or not? -Shawn |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
revDAVE wrote:
> Newbie - is there a function similar to the sql 'like' comparison operator? > > I would like to be able to compare 2 strings: > > If $this ---*like or similar to*--- $that > > That type of thing... I strongly suggest you read up on regular expressions: http://uk.php.net/manual/en/book.regex.php Knowing how to use regular expressions is a a very handy skill. I do a lot of mass changing of files via the command line with tools such as grep, sed and awk and a mastery regular expressions can save hours of laborious typing and find/replacing ![]() Reminds me of one of my favourite xkcd cartoons: http://xkcd.com/208/ Col |
|
![]() |
| Outils de la discussion | |
|
|