Jump to content

[SOLVED] Strpos Function Won't Work


Recommended Posts

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

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.

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

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!  ;D

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.