strpos always FALSE

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

i92guboj
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Jul 31, 2013 2:13 am

strpos always FALSE

Postby i92guboj » Wed Jul 31, 2013 2:29 am

Hello, good morning!

I am new here, and not too skilled with php (just so you know).

I have a problem with strpos(). And not, it's not due to the usage of '==' vs '==='.

The code is this:

Code: Select all

      // strings are different
      if(strcmp($thisstring, $prevstring) != 0) {

         print "---------------------------------<br/>";
         print "thisstring: ".$thisstring."<br/>";
         print "prevstring: ".$prevstring."<br/>";
         print "parent:     ".$parent."<br/>";
         
         $pos = mb_strpos($thisstring, $prevstring);

         if(mb_strpos($thisstring, $prevstring) === FALSE):
            print "mb_strpos returned FALSE<br/>";      
            // new string does not contain the previous one,
            // so, it's not its child
            $parent = $thisstring;
            print '<tr class="special_for_maria_special"><td colspan="4"><h4>' . $thisstring . '</h4></td></tr>';
         else:
            print "strpos said pos >=0<br/>";
                        
            // the new string contains the previous one,
            // so, the new string is a child of $prevstring
            // $parent is already well set
            $tmpstring = $thisstring;
            print '<tr class="special_for_maria"><td colspan="4"><h4>' . $prevstring . '</h4></td></tr>';
         endif;
         // in any case, set $prevstring = $thisstring, so the next round goes ok
         $prevstring = $thisstring;
      }
      // else strings are the same, we do NOTHING
      ?>


The output is this:

Code: Select all

---------------------------------
thisstring: Gestion contable y gestion administrativa para auditoria
prevstring:
parent:
mb_strpos returned FALSE
---------------------------------
thisstring: Gestion contable y gestion administrativa para auditoriaContabilidad y fiscalidad
prevstring: Gestion contable y gestion administrativa para auditoria
parent: Gestion contable y gestion administrativa para auditoria
mb_strpos returned FALSE
---------------------------------
thisstring: Auditoria
prevstring: Gestion contable y gestion administrativa para auditoriaContabilidad y fiscalidad
parent: Gestion contable y gestion administrativa para auditoriaContabilidad y fiscalidad
mb_strpos returned FALSE
---------------------------------
thisstring: Ofimatica
prevstring: Auditoria
parent: Auditoria
mb_strpos returned FALSE


This code is inside a foreach loop, so it is run for a list of elements. It is intended to pick a specially crafted list and convert one field on each row into a header, to group the list by categories. It's irrelevant, nonetheless. The thing is that, regardless the strings being clearly coincident at the beginning, strpos is always returning false.

As you can see I am using the strong equality operator, so that's not it. I have used php only in small snippets in the past, but I know my way around C and C++. Still, it could be something simple that I am skipping here...

As you see, in that snippet I am using mb_strpos(), but I have also tried strpos() and iconv_strpos(). Same result.

I have been fighting with this for three days now and I am about to turn mad, so any help is appreciated :oops:

The url, in case you want to take a look is:
http://www.inanfor.com/index.php/certif ... sionalidad

Thanks beforehand for any guidance on this :)

EDITED: This is really starting to bug me, can anyone explain this to me:

Code: Select all

      $str='foobarmoocow';
      $rts='foo';
      
      if(strpos($srt,$rts) === false)
      {
         echo "Comparing vars said FALSE<br/>";
      }
      else
      {
         echo "Camparing vars said TRUE<br/>";
      }
      if(strpos("foobarmoocow","foo") === false)
      {
         echo "Comparing literals said FALSE<br/>";
      }
      else
      {
         echo "Camparing literals said TRUE<br/>";
      }


outputs:

Code: Select all

Comparing vars said FALSE
Camparing literals said TRUE


This must be something really stupid. That, or the php from my hosting is seriously messed up.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: strpos always FALSE

Postby johnj » Wed Jul 31, 2013 5:29 am

srt and str - you have a typo I guess.

i92guboj
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Jul 31, 2013 2:13 am

Re: strpos always FALSE

Postby i92guboj » Wed Jul 31, 2013 5:37 am

$str and $rts are different strings. Silly names that I used, I know.

The thing is that, when I use strpos($str,$rts) it returns one thing and when I use the strings directly the result is different.

In fact, strpos always returns false, no matter what $vars I use. :help:

Thank you anyway :)

i92guboj
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Jul 31, 2013 2:13 am

Re: strpos always FALSE

Postby i92guboj » Wed Jul 31, 2013 6:41 am

Oh, yes, you were right about that simple test case. It was my fault. I really got a headache out of this.

However, the problem in the original code stands still.

I can only conclude that the strings look the same but they are not the same.

Given my original code, and adding var_dumps onto it:

Code: Select all

      if(strcmp($thisstring, $prevstring) != 0) {
         var_dump($prevstring);
         echo "<br/>";
         var_dump($thisstring);
         echo "<br/>";
         
         $pos = mb_stripos($thisstring, $prevstring);

         if($pos === false)
         {
            print "mb_strpos returned FALSE<br/>";         
         }         
         else
         {
            print "mb_strpos returned TRUE >=0<br/>";
         }
         $prevstring = $thisstring;
      }


This is what I get :

Code: Select all

string(0) ""
string(208) "Gestion contable y gestion administrativa para auditoria"
mb_strpos returned FALSE
string(208) "Gestion contable y gestion administrativa para auditoria"
string(217) "Gestion contable y gestion administrativa para auditoriaContabilidad y fiscalidad"
mb_strpos returned FALSE
string(217) "Gestion contable y gestion administrativa para auditoriaContabilidad y fiscalidad"
string(129) "Auditoria"
mb_strpos returned FALSE
string(129) "Auditoria"
string(129) "Ofimatica"
mb_strpos returned FALSE


I should be getting NOT FALSE (an int, concretelly) in the second iteration, shouldn't I?

i92guboj
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Jul 31, 2013 2:13 am

Re: strpos always FALSE

Postby i92guboj » Wed Jul 31, 2013 7:19 am

Ok. I have done some char by char parsing, and both strings look the same, but they are <a.../>. The question now is why dump_var doesn't show anything about that. It surely is some html<>php interaction that I am not aware of. I am starting to pay the newbie quote here :P

I blindingly assumed that the output of dump_var would be valuable for anything, when it clearly is not. :oops:

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: strpos always FALSE

Postby johnj » Fri Aug 02, 2013 2:09 am

Please read about strpos and stripos from the php manual on php.net. Then, try a few simple examples.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 2 guests