/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] strpos question

I’m trying to use the strpos function to search a text file. I have no problems getting results if I use one word as my search term, but if I use more than one word, I get no results. Is strpos only capable of looking for one word in an array? Is there a better way to do this…. here is the basic code, $search is the array being searched:

$show = $_GET[‘show’];
if ($show==NULL) { $show=”00″;

//there are about 30 lines of code in between these exploding the file, then naming the subvariables in the file and looping through them to display. All items in the file are displayed if nothing is input for show, otherwise only the items with the word=show are displayed.

if ((strpos($search,$show)>=1)) { // Search for words
echo results // results is actually a table with the variable placements
};

This works great if only one word is input for show, but shazbots if more than one word is entered. Suggestions or links to tutorials etc are more than welcome.

Thank you very much!

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@chazzyMar 01.2006 — you should do it one at a time. it will search "search" for the literal of show.

Also, use the php tags (from the stick on top) and show us the code you are using. i don't see where you're setting search.
Copy linkTweet thisAlerts:
@bokehMar 01.2006 — [I][code=php]if ((strpos($search,$show)>=1)) {[/code][/I]Well for a start that is wrong. What if the match was at position 0? It should be [code=php]if (strpos($search,$show) !== FALSE) {[/code]Are you actually using the position info or just looking for a match. If you are just looking for a match a regex (eregi) would work fine. Just implode the array around a "|"
Copy linkTweet thisAlerts:
@NogDogMar 01.2006 — [code=php]
$searchWords = preg_split('/[s,]+/', $search);
$match = FALSE;
foreach($searchWords as $word)
{
// strpos() returns 0 if matches on first character, so use !== FALSE:
if(strpos($word, $show) !== FALSE)
{
$match = TRUE;
break;
}
}
if($match)
{
// match was found, show output
}
[/code]
Copy linkTweet thisAlerts:
@bokehMar 01.2006 — What I was getting at was why [I]strpos()[/I] was the chosen function. If it is only to look for a match something like the following would work fine:[code=php]<?php

$dodgyText = 'Lots of dodgy words in here';

$searchWords = 'string of words to find';

if(eregi('('.str_replace(' ', '|', $searchWords).')', $dodgyText, $matches))
{
echo 'Match found: '.$matches[0];
}
else
{
echo 'No matches found';
}

?>[/code]
Copy linkTweet thisAlerts:
@yolindaauthorMar 01.2006 — okay, I changed the code to NoDog's suggestion, but I am still only getting results for one of the search words, so I've included more of the surrounding code (I was trying to spare you...)in case there is something there

[code=php]
$show = $_GET['show'];
$show = ucwords($show);
$show = ucfirst(strtolower($show));

$values = explode("====", $data);
$numitems = count($values);

sort($values,SORT_NUMERIC);

$numpages = round($numitems/$showperpage);

$datotal=$dastart+$showperpage;
if ($datotal>=$numitems) { $datotal=$numitems; }

$dacount=1;
$count2=0;
$current=0;

while ($dacount-1 < $numitems) {

$subvalues = explode("rn", $values[($dacount+$dastart)]);

$height = $subvalues[0];
$item = $subvalues[1];
$title = $subvalues[2];
$notes = $subvalues[3];

$search = $item . $title;

$searchWords = preg_split('/[s,]+/', $search);
$match = FALSE;
foreach($searchWords as $word)
{
// strpos() returns 0 if matches on first character, so use !== FALSE:
if(strpos($word, $show) !== FALSE)
{
$match = TRUE;
break;
}
}
if($match)
{
// match was found, show output

}
$count2=$count2+1;
$current=1;
and so forth...
}

[/code]
Copy linkTweet thisAlerts:
@NogDogMar 01.2006 — My preg_split() was assuming the words would be separated by white-space and/or commas. For it to work, you'll need to change:
[code=php]
$search = $item . $title;
[/code]

...to something like:
[code=php]
$search = $item . "," . $title;
[/code]

Also, you'll want to replace the comment in the [b]if($match)[/b] block to actually do something if you find a match.
Copy linkTweet thisAlerts:
@yolindaauthorMar 01.2006 — Nogdog, I do have a display instead of the comment, I just left it as the comment for posting here. The display is fine as long as I only have one word for the search, but it doesn't display any results if I use more than one word. I made the change you have above, and still only works with one word. I've really been pulling my hair out on this one, but I'll keep pounding away.... I'm going to try one of the other suggestions to see if they work.

Thank you everyone, I really do appreciate your help!!
Copy linkTweet thisAlerts:
@yolindaauthorMar 01.2006 — I just want to say thank you to everyone, I got it using Bokeh's example.... thank you very much!
×

Success!

Help @yolinda spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...