473,386 Members | 1,785 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,386 software developers and data experts.

parse a string for certain words

Hi all, I was wondering if php can parse a text string for certain words and
return "true" if that word is found. For example, I have a string like this:

$string = "The rain in spain is the same as the rain on the plain";

I want to run a php command(s) that will parse the sentence and return true
if, for example, the word "spain" is found.

Any ideas?

Thanks,

Mosher
Jul 17 '05 #1
9 3543
NC
Mosher wrote:

I was wondering if php can parse a text string for certain words and
return "true" if that word is found.


You shouldn't be wondering. Reading The PHP Manual is considered
by many to be a more efficient use of time. This should get you
started:

http://www.php.net/strpos

Cheers,
NC

Jul 17 '05 #2
Mosher wrote:
Hi all, I was wondering if php can parse a text string for certain words and
return "true" if that word is found. For example, I have a string like this:

$string = "The rain in spain is the same as the rain on the plain";

I want to run a php command(s) that will parse the sentence and return true
if, for example, the word "spain" is found.


preg_match('`\bspain\b`',$subject)

Explanation: Match 'spain' if the characters before and
after are not word characters, where 'word characters' are
letters, numbers and underscores. Or you could make your
own assertions; e.g.,

`(?<![a-zA-Z-])spain(?![a-zA-Z-)`

matches 'spain' where the characters before and after are
not US-ASCII letters or hyphens.

Make the pattern case insensitive by setting the i modifier.

preg_match('`(?<![a-zA-Z-])spain(?![a-zA-Z-)`i',$subject)

--
Jock
Jul 17 '05 #3
Both strpos() and preg_match() will work, but in this case strpos() is
preferred for performance reasons. From
<http://www.php.net/manual/en/function.preg-match.php> :

"Tip: Do not use preg_match() if you only want to check if one string
is contained in another string. Use strpos() or strstr() instead as
they will be faster."

Jul 17 '05 #4
NC, you shouldn't assume that I didn't do some preliminary reading...Your
sarcasm to what appears to be a simple question is unnecessary. A simple
"check out strpos" would have been sufficient...

Thanks anyway,

Mosher
"NC" <nc@iname.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Mosher wrote:

I was wondering if php can parse a text string for certain words and
return "true" if that word is found.


You shouldn't be wondering. Reading The PHP Manual is considered
by many to be a more efficient use of time. This should get you
started:

http://www.php.net/strpos

Cheers,
NC

Jul 17 '05 #5
ze********@gmail.com wrote:
Both strpos() and preg_match() will work, but in this case strpos() is
preferred for performance reasons.


I'm sure the difference in speed is noticeable, so thank you
for bringing it up.

The point here, though, was to match words out of a string
(a sentence), not strings out of a string. Since strpos()
cannot tell a word from a string, regular expressions are a
better choice.

--
Jock
Jul 17 '05 #6
You are correct, and preg_match does give you a bit more versatility.

One could also use explode() and in_array to search for words in some
sort of delimited string (like a sentence where the delimiter is a
space). Something like:

$string = "The rain in spain is the same as the rain on the plain";
$searchWord = "spain";
return in_array($searchWord, explode(" ", $string));

Jul 17 '05 #7
ZeldorBlat wrote:
One could also use explode() and in_array to search for words in some
sort of delimited string (like a sentence where the delimiter is a
space). Something like:

$string = "The rain in spain is the same as the rain on the plain";
$searchWord = "spain";
return in_array($searchWord, explode(" ", $string));


Remember that while exploding the string on a single
character should work for simple sentences, such as 'The
rain in Spain ... ', it will likely fail for more complex
ones containing internal punctuation, such as the one you're
reading now.

Another difficulty with parsing sentences into words is that
the hyphen might be part of a word or it might join compound
adjectives, which you might consider separate words.

--
Jock
Jul 17 '05 #8
Hey guys, thanks. I used the explode() function and it worked for this
example.

Just an FYI, using the explode() method is space delimited, so if I had a
string like: "the rain in spain is the same as st paul", st paul would be
two elements in the array, not one.

Mosher

"Mosher" <mo***********@yahoo.com> wrote in message
news:fP********************@comcast.com...
Hi all, I was wondering if php can parse a text string for certain words
and return "true" if that word is found. For example, I have a string like
this:

$string = "The rain in spain is the same as the rain on the plain";

I want to run a php command(s) that will parse the sentence and return
true if, for example, the word "spain" is found.

Any ideas?

Thanks,

Mosher

Jul 17 '05 #9
Explode also allows you to specify a string to use for the boundaries.
Unfortunately, you can't say something like "explode using either a
space or a newline as the seperator." However, you can use strtok() in
this situation:

http://www.php.net/manual/en/function.strtok.php

It will tokenize the string splitting it on any single character from
the second parameter ("token"). You can even change your delimiters
with each call to strtok. This might also save you some time because
if you only care whether any word exists in a string, you can quit as
soon as you find it. Something like this (a contrived example, of
course):

$string = "The rain-in*spain is the same as the rain-on*the-plain";
$matches = array("rain", "spain", "plain");
$delims = " -*";

$tok = strtok($string, $delims);
while($tok) {
if(array_search($tok, $matches) !== false)
return true;
}
return false;

Jul 17 '05 #10

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

Similar topics

5
by: Theresa Hancock via AccessMonster.com | last post by:
I have an Excel table I need to import into Access. The name is entered into one field "Name". I'd like to have two fields in Access, FirstName and LastName. How do I do this. -- Message posted...
12
by: MLH | last post by:
I have a bunch of what you see below in a field named CSZ. I want to parse it the easiest way possible. I just don't know where to start... CSZ Anchorage AK 99518-3051 Anchorage AK 99518-3051...
3
by: Bob | last post by:
What I want to do is write a program that reads through a Word Document, finds certain words or sentences I want, and then paste into an Excel spreadsheet. I dont know much about C#. But I...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
2
by: Raterus | last post by:
Hi, I'm looking for ideas for the most efficient way to accomplish this. I have a string representing names a person goes by. "John Myers Joe John Myers" And I need to parse it in such a...
7
by: Matt | last post by:
I am attempting to reformat a string, inserting newlines before certain phrases. For example, in formatting SQL, I want to start a new line at each JOIN condition. Noting that strings are...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.