Newbie - is there a function similar to the sql 'like' comparisonoperator?

This is a discussion on Newbie - is there a function similar to the sql 'like' comparisonoperator? within the PHP General forums, part of the PHP Programming Forums category; Newbie - is there a function similar to the sql 'like' comparison operator? I would like to be able to compare ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 4 Days Ago
revDAVE
 
Posts: n/a
Default Newbie - is there a function similar to the sql 'like' comparisonoperator?

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]



Reply With Quote
  #2 (permalink)  
Old 4 Days Ago
Kalle Sommer Nielsen
 
Posts: n/a
Default Re: [PHP] Newbie - is there a function similar to the sql 'like' comparison operator?

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


Reply With Quote
  #3 (permalink)  
Old 4 Days Ago
Shawn McKenzie
 
Posts: n/a
Default Re: Newbie - is there a function similar to the sql 'like' comparisonoperator?

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
Reply With Quote
  #4 (permalink)  
Old 4 Days Ago
Colin Guthrie
 
Posts: n/a
Default Re: Newbie - is there a function similar to the sql 'like' comparisonoperator?

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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:44 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0