PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Is there any function in php that will match a word exactly and if it finds it, it returns true.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is there any function in php that will match a word exactly and if it finds it, it returns true.

Réponse
 
LinkBack Outils de la discussion
Vieux 10/11/2007, 21h09   #1
ross.oneill@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Is there any function in php that will match a word exactly and if it finds it, it returns true.

Hi,

Is there any function in php that will match a word exactly and if it
finds it, it returns true.

For example if I search for "CA"

strVar = "Bob is from Los Angeles CA" - return true

strVar "Bob is from Canada" -- returns false

Any is appreciated

Thanks

-Ross

  Réponse avec citation
Vieux 10/11/2007, 21h17   #2
Lars Eighner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

In our last episode,
<1194728954.083106.150790@c30g2000hsa.googlegroups .com>,
the lovely and talented ross.oneill@gmail.com
broadcast on comp.lang.php:

> Hi,


> Is there any function in php that will match a word exactly and if it
> finds it, it returns true.



Yes.


--
Lars Eighner <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 436 days to go.
What do you do when you're debranded?
  Réponse avec citation
Vieux 10/11/2007, 21h37   #3
Krustov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

<comp.lang.php>
<>
<Sat, 10 Nov 2007 13:09:14 -0800>
<1194728954.083106.150790@c30g2000hsa.googlegroups .com>

> Is there any function in php that will match a word exactly and if it
> finds it, it returns true.
>
> For example if I search for "CA"
>
> strVar = "Bob is from Los Angeles CA" - return true
>
> strVar "Bob is from Canada" -- returns false
>


$demo="Bob is from Los Angeles CA";

$qaz="CA";

$wsx=strpos($qaz,$demo);

if ($wsx==true) {print "exact match found";}



NOTE: untested and you may need to play around with it .
  Réponse avec citation
Vieux 10/11/2007, 22h05   #4
ross.oneill@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

Thanks for your response. But if in your example
strVar "Bob is from Canada" -- would return true when I want it to
return false.

  Réponse avec citation
Vieux 10/11/2007, 22h43   #5
Brendan Gillatt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly andif it finds it, it returns true.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ross.oneill@gmail.com wrote:
> Thanks for your response. But if in your example
> strVar "Bob is from Canada" -- would return true when I want it to
> return false.
>

*borrowing Krustov's code as template*

$demo="Bob is from Los Angeles CA";

$qaz=" CA"; // note the extra space

$wsx=strpos($qaz,$demo);

if ($wsx==true) {print "exact match found";}


- --
Brendan Gillatt
brendan {at} brendangillatt {dot} co {dot} uk
http://www.brendangillatt.co.uk
PGP Key: http://pgp.mit.edu:11371/pks/lookup?...rch=0xBACD7433
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)

iD8DBQFHNjP4kA9dCbrNdDMRAnQ9AJ0dL6C23gPRQ4n40hjqIX g+H5nEQwCg0Yum
r9nv+WwcsnZfQIC22bQCdlU=
=uQxP
-----END PGP SIGNATURE-----
  Réponse avec citation
Vieux 10/11/2007, 22h51   #6
Krustov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

<comp.lang.php>
<Brendan Gillatt>
<Sat, 10 Nov 2007 22:43:04 +0000>
<uu-dnYh9Kt09rqvanZ2dnUVZ8sLinZ2d@pipex.net>

> *borrowing Krustov's code as template*
>
> $demo="Bob is from Los Angeles CA";
>
> $qaz=" CA"; // note the extra space
>
> $wsx=strpos($qaz,$demo);
>
> if ($wsx==true) {print "exact match found";}
>


Thats not a ideal solution to the well known php bug you refer to .

Best not to mention such things until a user has a problem IMHO .
  Réponse avec citation
Vieux 11/11/2007, 00h45   #7
The Natural Philosopher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly andif it finds it, it returns true.

Brendan Gillatt wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> ross.oneill@gmail.com wrote:
>> Thanks for your response. But if in your example
>> strVar "Bob is from Canada" -- would return true when I want it to
>> return false.
>>

> *borrowing Krustov's code as template*
>
> $demo="Bob is from Los Angeles CA";
>
> $qaz=" CA"; // note the extra space
>
> $wsx=strpos($qaz,$demo);
>
> if ($wsx==true) {print "exact match found";}
>


How about "CAnada".

Faced with this sort of thing in 'C' I decided that learning to write a
regexp when I could already write C was bollocks:

You have to decide what is allowable before and after the 'CA" that
makes it a complete word, not part of something else.

In C the ispunct(), isspace() and '\0' macros proved useful.

>
> - --
> Brendan Gillatt
> brendan {at} brendangillatt {dot} co {dot} uk
> http://www.brendangillatt.co.uk
> PGP Key: http://pgp.mit.edu:11371/pks/lookup?...rch=0xBACD7433
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.3 (MingW32)
>
> iD8DBQFHNjP4kA9dCbrNdDMRAnQ9AJ0dL6C23gPRQ4n40hjqIX g+H5nEQwCg0Yum
> r9nv+WwcsnZfQIC22bQCdlU=
> =uQxP
> -----END PGP SIGNATURE-----

  Réponse avec citation
Vieux 11/11/2007, 01h41   #8
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

On Nov 10, 5:51 pm, Krustov <m...@privacy.net> wrote:
> <comp.lang.php>
> <Brendan Gillatt>
> <Sat, 10 Nov 2007 22:43:04 +0000>
> <uu-dnYh9Kt09rqvanZ2dnUVZ8sLin...@pipex.net>
>
> > *borrowing Krustov's code as template*

>
> > $demo="Bob is from Los Angeles CA";

>
> > $qaz=" CA"; // note the extra space

>
> > $wsx=strpos($qaz,$demo);

>
> > if ($wsx==true) {print "exact match found";}

>
> Thats not a ideal solution to the well known php bug you refer to .
>
> Best not to mention such things until a user has a problem IMHO .


And which well known bug is that?

  Réponse avec citation
Vieux 11/11/2007, 01h43   #9
mik3l3374@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

On Nov 11, 5:37 am, Krustov <m...@privacy.net> wrote:
> <comp.lang.php>
> <>
> <Sat, 10 Nov 2007 13:09:14 -0800>
> <1194728954.083106.150...@c30g2000hsa.googlegroups .com>
>
> > Is there any function in php that will match a word exactly and if it
> > finds it, it returns true.

>
> > For example if I search for "CA"

>
> > strVar = "Bob is from Los Angeles CA" - return true

>
> > strVar "Bob is from Canada" -- returns false

>
> $demo="Bob is from Los Angeles CA";
>
> $qaz="CA";
>
> $wsx=strpos($qaz,$demo);
>
> if ($wsx==true) {print "exact match found";}
>
> NOTE: untested and you may need to play around with it .

using strpos, this would also match

$demo="Bob is from Los Angeles CAblahblah";

while i think OP wanted exact match. Correct me if i am wrong.

maybe something like this:

$demo="Bob is from Los Angeles CA ddfs";
$s = split(" ",$demo);
foreach ($s as $k)
{
if( $k === "CA" )
{
echo "Found CA: $k\n";
}
}

  Réponse avec citation
Vieux 11/11/2007, 15h03   #10
cameron7
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

This might work.

<?php
function myfunc($string,$pattern)
{
if(strpos(' '.$string.' ', ' '.$pattern.' ') !== FALSE)
{
return true;
}
return false;
}

var_dump(myfunc('Bob is from Los Angeles CA','CA'));
var_dump(myfunc('Bob is from Canada','CA'));
?>



On Nov 10, 4:09 pm, ross.one...@gmail.com wrote:
> Hi,
>
> Is there any function in php that will match a word exactly and if it
> finds it, it returns true.
>
> For example if I search for "CA"
>
> strVar = "Bob is from Los Angeles CA" - return true
>
> strVar "Bob is from Canada" -- returns false
>
> Any is appreciated
>
> Thanks
>
> -Ross



  Réponse avec citation
Vieux 11/11/2007, 20h19   #11
Aaron Saray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.

On Nov 10, 3:09 pm, ross.one...@gmail.com wrote:
> Hi,
>
> Is there any function in php that will match a word exactly and if it
> finds it, it returns true.
>
> For example if I search for "CA"
>
> strVar = "Bob is from Los Angeles CA" - return true
>
> strVar "Bob is from Canada" -- returns false
>
> Any is appreciated
>
> Thanks
>
> -Ross


You might use preg_match() with word boundaries... perhaps?

  Réponse avec citation
Vieux 12/11/2007, 17h51   #12
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.


<ross.oneill@gmail.com> wrote in message
news:1194732358.847042.123900@50g2000hsm.googlegro ups.com...
> Thanks for your response. But if in your example
> strVar "Bob is from Canada" -- would return true when I want it to
> return false.


people shy away from regex...but, i don't know why.

/\bca(\b|$)/i

preg_match 'Bob is from Canada' returns false

preg_match 'Bill is from San Diego, CA' returns true.



you'd be hard-pressed to write anything more simple or manageable.


  Réponse avec citation
Vieux 12/11/2007, 17h54   #13
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there any function in php that will match a word exactly and if it finds it, it returns true.


"Brendan Gillatt" <brendanREMOVETHIS@brendanREMOVETHISgillatt.co.u k> wrote
in message news:uu-dnYh9Kt09rqvanZ2dnUVZ8sLinZ2d@pipex.net...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> ross.oneill@gmail.com wrote:
>> Thanks for your response. But if in your example
>> strVar "Bob is from Canada" -- would return true when I want it to
>> return false.
>>

> *borrowing Krustov's code as template*
>
> $demo="Bob is from Los Angeles CA";
>
> $qaz=" CA"; // note the extra space
>
> $wsx=strpos($qaz,$demo);
>
> if ($wsx==true) {print "exact match found";}


ummm...don't you need $wsx === true anyway?!


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h12.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,38344 seconds with 21 queries