NarrowPathPilgrim Posted June 2, 2007 Share Posted June 2, 2007 I tried the strpos function with digits and it worked fine, but when I use variables it quit working. <?php ... foreach ($each_letter as $letter){ foreach ($wordlist as $word){ if (strpos($word, $letter) == $jump){ $finalwords[] = $word." - ".$letter; } } } ... ?> This should return a large list of results, the $wordlist array has many words which contain the letter in the $letter variable at the position in the $jump variable. Any help is appreciated! Thanks in Advance, Zach Doty PS. Its been a good six months or more since I've done PHP scripting last... I might have missed something basic. Link to comment https://forums.phpfreaks.com/topic/53959-solved-strpos-function-wont-work/ Share on other sites More sharing options...
Orio Posted June 2, 2007 Share Posted June 2, 2007 There's nothing wrong with your code, but there must be a problem with the logic or something. First of all, make sure $each_letter is an array of letters. Also keep in mind that strpos() returns values from 0 and not from 1. Except that, make sure $jump is defined correctly. Orio. Link to comment https://forums.phpfreaks.com/topic/53959-solved-strpos-function-wont-work/#findComment-266757 Share on other sites More sharing options...
NarrowPathPilgrim Posted June 2, 2007 Author Share Posted June 2, 2007 There's nothing wrong with your code, but there must be a problem with the logic or something. First of all, make sure $each_letter is an array of letters. It is, I'm using this: $mytext = "Test"; $each_letter = str_split($mytext); Also keep in mind that strpos() returns values from 0 and not from 1.Yep, know that. Except that, make sure $jump is defined correctly.I defined $jump on line 3. $jump = "0"; $wordlist is a valid array, and $finalwords is set as an array also. Anything else you can think of? The PHP file returns no results. Thanks, Zach Doty Link to comment https://forums.phpfreaks.com/topic/53959-solved-strpos-function-wont-work/#findComment-266765 Share on other sites More sharing options...
NarrowPathPilgrim Posted June 2, 2007 Author Share Posted June 2, 2007 Oh, uh, I fixed it! I'm not sure how they got there, but there were two // lines before my echo command at the bottom of my script. 2:08 AM...it must be too late! So thats fixed, but now I've got another problem. The $finalwords array is returning many values that don't contain the needle (in this case, "T" from my word "Test"). My first few lines from the page look like this ($wordlist is a dictionary word list...): ad - T am - T an - T as - T at - T ax - T be - T by - T Any ideas? I thought about trying this: <?php ... if (strpos($word, $letter) == $jump && strpos($word, $letter) == true){ ... ?> But then the script returns no results (and yes, it does echo this time!). Sincerely, Zach Doty Link to comment https://forums.phpfreaks.com/topic/53959-solved-strpos-function-wont-work/#findComment-266776 Share on other sites More sharing options...
NarrowPathPilgrim Posted June 2, 2007 Author Share Posted June 2, 2007 Ah, finally got it (used !== false instead of == true...). Sorry for all the trouble! Sincerely, Zach Doty Link to comment https://forums.phpfreaks.com/topic/53959-solved-strpos-function-wont-work/#findComment-266779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.