Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.