473,241 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 help is appreciated

Thanks

-Ross

Nov 10 '07 #1
12 2636
In our last episode,
<11**********************@c30g2000hsa.googlegroups .com>,
the lovely and talented ro*********@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?
Nov 10 '07 #2
<comp.lang.php>
<>
<Sat, 10 Nov 2007 13:09:14 -0800>
<11**********************@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 .
Nov 10 '07 #3
Thanks for your response. But if in your example
strVar "Bob is from Canada" -- would return true when I want it to
return false.

Nov 10 '07 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ro*********@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-----
Nov 10 '07 #5
<comp.lang.php>
<Brendan Gillatt>
<Sat, 10 Nov 2007 22:43:04 +0000>
<uu******************************@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 .
Nov 10 '07 #6
On Nov 10, 5:51 pm, Krustov <m...@privacy.netwrote:
<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?

Nov 11 '07 #7
On Nov 11, 5:37 am, Krustov <m...@privacy.netwrote:
<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";
}
}

Nov 11 '07 #8
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 help is appreciated

Thanks

-Ross

Nov 11 '07 #9
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 help is appreciated

Thanks

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

Nov 11 '07 #10
Brendan Gillatt wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ro*********@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-----
Nov 12 '07 #11

<ro*********@gmail.comwrote in message
news:11**********************@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.
Nov 12 '07 #12

"Brendan Gillatt" <br***************@brendanREMOVETHISgillatt.co.ukw rote
in message news:uu******************************@pipex.net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ro*********@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?!
Nov 12 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
5
by: Richard A. DeVenezia | last post by:
Hi: function foo () { var xyz = 123; function bar () { var abc = 456; } } I can alert (foo) to see the function source
6
by: Ryan Muller | last post by:
My company just upgraded from Access 97 to Access 2003 today and we are having some issues in a database that generates a Word document from information selected in a form. Here is the code we...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
3
by: Russell | last post by:
Hey, ok i have numerous tables to search through for a 'site search'. some of the searchble fields have html embeded within so after some quick referencing, saw I can use the regExp function...
4
by: hanseymoon | last post by:
Dear newsgroup: I've got this long function, which works good overall to spell check words from a dictionary and I am not in a position to replace it. Can someone please see where or how it...
18
Dököll
by: Dököll | last post by:
Here it goes again, thanks again for responding: 'PROPOSED SOLUTION 2 'I wanted to not creatre a file and just read straight out of Text6.Text and 'output my findings in legal text boxes 'I...
6
by: lawrence k | last post by:
Wierd. Go to this page: http://www.ihanuman.com/search.php and search for "yoga" This query gets run: SELECT * FROM albums WHERE MATCH(name,description) AGAINST ('yoga') ORDER BY id DESC
6
by: Andrus | last post by:
I need to implement vfp function which uses * and ? wildcards: static bool Like( cExpression1, cExpression2) cExpression1 Specifies the character expression that Like( ) compares with...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.