Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One more... 3

Status
Not open for further replies.

vbkris

Programmer
Joined
Jan 20, 2003
Messages
5,994
Location
IN
o.k lets say i have a sentence:
"Mary had a little black lamb."

i search for the word "little" i want to display 1 word before and after "little". ie
a little black

any takers?

Known is handfull, Unknown is worldfull
 
Messy way of doing it - find the character position of the first space after "little" and the following space using the strpos() function. Then display the value of the string between the two values using substr() function. Then do the same to find the word preceeding "little" using a combination of strrpos() and substr() functions.

Can't think of an easier way, sorry!

Chris
 
cmhunt:
ur method is what i tried. finding a space after the word is no problem. can u tell me how to find a word before "little" using strpos?

Known is handfull, Unknown is worldfull
 
Try this, seems to work fine:
Code:
<?php
$string=&quot;Mary had a little black lamb&quot;;
$findme=&quot;little&quot;;
$searchpos=strpos($string,$findme);
$searchspace=strpos($string,&quot; &quot;,$searchpos);
$searchspace2=strpos($string,&quot; &quot;,$searchspace+1);
$searchspace3=strrpos(substr($string,0,$searchpos-1),&quot; &quot;);
print substr($string,$searchspace3,$searchspace2-$searchspace3);
?>
 
Try
Code:
if (preg_match(&quot;/ ([^ ]+little[^ ]+) /&quot;, $string, $matches))
    $result = $matches[1];

//Daniel
 
This one should work better:
Code:
if (preg_match(&quot;/[^ ]+ little [^ ]+/&quot;, $string, $matches))
    $result = $matches[0];

//Daniel
 
danielhozac:

You beat me to the punch. I was just about to post:

Code:
<?php
$mary = &quot;Mary had a little black lamb&quot;;

preg_match ('/\w+ little \w+/', $mary, $results);

print $results[0];
?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
i will try that and come back...

Known is handfull, Unknown is worldfull
 
Alternative:
Have you considered exploding the sentence into an array?
1. explode on the spaces
2. ascertain the key for 'little'
3. concatenate the key-1,key,key+1

However, the regex is better.
 
o.k fellas, that bit of code worked gr8. sorry for the late reply, but i could get to this only now.

but then i have some more problems:
1. How do i make it case insensitive?
2. I want 5 words before and after. and if there are no words before and after the search string then only that word must be displayed.

hope u understand my problem.

i am writing a script like a search engine (very amaeturish, no databases, as of now just reading the files and check for occurences.)



Known is handfull, Unknown is worldfull
 
and cmhunt:
great piece of code. i must have been dozing...

Known is handfull, Unknown is worldfull
 
cheers guys found that out myself (i modifier)...

Known is handfull, Unknown is worldfull
 
but the words problem is not solved...

Known is handfull, Unknown is worldfull
 
Try
Code:
if (preg_match(&quot;/(\w+ ){0,5}$term( \w+){0,5}/i&quot;, $string, $matches))
    echo $matches[0];

//Daniel
 
adn can u explain how it works?

Known is handfull, Unknown is worldfull
 
Code:
(\w+ )
= 1 or more &quot;word&quot; characters followed by a space...
Code:
{0,5}
= ... repeated at most 5 times.

//Daniel
 
<sarcasm>
It's a regular expression. By definition, it works because it's voodoo.
</sarcasm>

/(\w+ ){0,5}$term( \w+){0,5}/i

&quot;\w+&quot; is basically a word. One or more word characters (any letter or digit or the underscore character).
&quot;(\w+ )&quot; is a word plus its following space.
&quot;( \w+)&quot; is a word plus its preceding space (or more correctly a space and its following word).
&quot;{0,5}&quot; means find at least zero and at most 5 of the preceding expression, which is &quot;(\w+ )&quot;, or a word with its following space.

In English, one possible way of describing what the expression is looking for:
Look for as few as zero and as many as 5 examples in a rows of a word, each followed by a space, then look for whatever is in term, then look for as few as zero and as many as 5 examples in a row of spaces each followed by words.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
thanks...

i have read a lot of tutorials online. can u give me some links (not official php site or devshed. i have read all of them and am now considerably confused)...

Known is handfull, Unknown is worldfull
 
I dunno. I learned them when I had a perl project dumped in my lap once. I didn't know perl at the time.

Since danielhozac and I are both talking about perl-compatible regular expressions, you might widen your search to include tutorials on the use of regular expressions in perl.

Zend has a tutorial:



Want the best answers? Ask the best questions: TANSTAAFL!!
 
o.k. back i go to the basics... :)

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top