-1

$target is a char and I am trying to find the last occurrence of that char in $line. I get -1 for every single output even if I am certain the $target does exist within $line at some index.

    $fh = fopen($someFile, "r");
    while (!feof($fh)) {
    $test = fgets($fh);
    $words = explode(",", $test);
    $line = $words[0];
    $target = $words[1];

    $answer = strrpos($line, $target);

    if ($answer !== false) {
    echo $answer;
    }
    else echo -1;
    echo "\n";
    }

This code returns -1 for every single value. If I change the $line to $test in the strrpos function it can find the index every time. I checked $line to make sure it is not empty and it is in fact the first part of the string. Why doesn't this work?

STLCards77
  • 121
  • 14
  • where are you setting `$answer`? – nl-x Jul 10 '14 at 07:30
  • probably `$answer = strrpos ($words[0], $words[1]);` ... – MarcoS Jul 10 '14 at 07:32
  • @MarcoS Well, if he forgets to actually do that, he wil indeed always get -1 – nl-x Jul 10 '14 at 07:32
  • Well, not even -1, strrpos returns FALSE if no match is found... – MarcoS Jul 10 '14 at 07:53
  • @MarcoS he will always get -1, because his simply states `else echo -1;` with an `if()` that will never be true. – nl-x Jul 10 '14 at 07:54
  • Yes: he (she?) will get "-1" in output, since he did not correctly assign $answer. But, after assigning it, he will have to check for `$answer !== FALSE`, to get a reasonable result... :-) – MarcoS Jul 10 '14 at 08:05

2 Answers2

0

I don't know if I did correctly understand your answer... However, you have some errors, in your code:

1) you did not assign the result of strrpos to $answer (probably just a typo...).
2) you did test function result for -1, but that function returns FALSE if no match is found.

This should work:

<?php
  $someFile = "data";
  $fh = fopen($someFile, "r");
  while (!feof($fh)) {
    $test = fgets($fh);
    $words = explode(",", $test);
    $line = $words[0];
    $target = $words[1];

    $answer = strrpos($line, $target);
    if ($answer !== FALSE) {
      echo $answer;
    } else {
      echo -1;
    }
    echo "\n";
  }
?>

With this data file:

haystack,stack,

this code prints:

3

which is supposedly the answer you're looking for...

Before going to production (:-) you should also be aware that:

  • feof() should be checked after fgets(), not before...
  • always use chop() on fgets()'s, to avoid newline issues (fgets return the newline, too...)

UPDATE
To better reflect your use case (I hope), and to implement all advises I wrote in the comments, here is an updated version of workinkg code. Please post your data if still should be not working for you...

File "test.php":

<?php
  $someFile = "data";
  $fh = fopen($someFile, "r");
  while (1) {
    $test = fgets($fh);
    if (feof($fh)) break;
    $test = chop($test);
    $words = explode(",", $test);
    $line = $words[0];
    $target = $words[1];

    $answer = strrpos($line, $target);
    if ($answer !== FALSE) {
      echo $answer;
    } else {
      echo -1;
    }
    echo "\n";
  }
?>

File "data":

haystack,s

run:

$ php test.php
3
MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • Thank you for excusing my typo and for the extra advice. In the process of converting some Java puzzle solutions to PHP and gain more practice. I STILL get -1 for every answer with this exact code, even if I know the $target exists in $line. $target is a single character and I am trying to find the last occurrence of that char in $line if that info helps. I really don't understand why it won't pick up the first if clause. – STLCards77 Jul 10 '14 at 20:25
  • Can you post your data file? – MarcoS Jul 11 '14 at 05:58
0

It is unclear what you are asking. Explode does not put the second element inside the first one, like you are suggesting in your question. Also in your question you evaluate $answer in your if(), but you don't set $answer anywhere in the code you show us.

But just to clarify:

$string = "abcd,efghij,kl";
$array = explode(",",$string);
echo $array[0]; // abcd
echo $array[1]; // efghij
echo $array[2]; // kl

If you need to know how big any of the elements are, or when the next elements start, just use strlen():

echo strleng($array[0]); // 4
echo strleng($array[1]); // 6
echo strleng($array[2]); // 2

As $array[0] is 4 characters long, and we have a comma after it, $array[1] starts at index 4+1 = 5.

And $array[2] starts at index 12 because the first 2 elements with the 2 commas are 4+1+6+1 = 12.

nl-x
  • 11,762
  • 7
  • 33
  • 61