Re: Unable to get strpos() to work correctly in class method

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Feb 26, 2015, at 7:42 AM, JEofVA wrote:

I have written a class with a method that uses the strpos() function, but I can't get it to work. However, when I use the method as a function in a simple test program that uses the identical code to simulate the class method (and feed it the same strings) it works exactly like I expect.

Below is an abbreviated version of the non-working method version. However, both versions are identical to this point, only this doesn't work while the test program version does. The problem is that the
while-loop never executes because $start always comes up false (but
only in the class version).

protected function extract_quotes($string)
// Assumes if $string has quotes they are an even number of them
//  Uses class property $keywrd_frags to store parsed fragments
{
   $delm = chr(34); // quote char in this case
   // start parsing from first character of incoming query string
   $start = strpos($string, $delm, 0);  //<=== POINT OF FAILURE
   while($start !== FALSE)
   {
   	  // the while loop extracts all the delimited blocks of text
   	  // one at a time until there are  none left.  At that point
   	  // $start will be false and $string will have only
	  //  un-delimited text (or an empty string)
	
    	     //This section never runs because $start is always FALSE

   }
       // No more delimited text, so return the remaining $string
   return $string;
}

What am I overlooking in the class version that keeps the code from working there?

Tried: I've tried 37 dozen different variations on this theme, including dropping the $delim variable and using '"' in the strpos() function. I've also used different delimiters, and even changed the delimiter character once the string got into the extract_quotes() function. No difference.

Running:
Apache Version :    2.2.17
PHP Version :    5.3.3


Thanks for any ideas,

Jonathan

As I read the php manual, if your are starting the search for the first position of the specified character from the beginning of the string, you don't need the third parameter. However make sure that the quote characters in the string are actually the quote character you specified as $deim. The quote characters in the string could be from a different char set.. The string position will be an integer, so testing for !-- FALSE is testing for a boolean specifically
and not an integer. I would do:
while ($start > -1)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux