Viewed   76 times

I'm using strpos to find the position of a string in another string. I first check if the string is found at all in there. Here's my line:

if (strpos($grafik['data'],$ss1)<>false && strpos($grafik['data'],$ss2)<>false && strpos($grafik['data'],$ss1) < strpos($grafik['data'],$ss2))

I check if both strings are contained and then I want the first one to be placed before the second one. In the php manual it says that strpos returns false when string is not found. However if my string starts at the zero position (strpos returns 0 since its the beginning), it seems like this statement

strpos($grafik['data'],$ss1)<>false

is false. Somehow 0==false ? How do I make the statement true when strpos returns 0 ?

 Answers

4

From http://www.php.net/manual/en/function.strpos.php:

Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

You have to use the === operator instead of ==.

In your case, instead of using <>, use !==:

strpos($grafik['data'], $ss1) !== false

This will return TRUE if $ss1 is found in $grafik['data']

Saturday, December 17, 2022
 
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

[email protected]:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
5

From the manual entry of strpos() function:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

which means that if($Postion !== TRUE) will always be true, as strpos() never returns true.

To make your function work as expected, change your if statement to if($Postion === false).

Sunday, August 21, 2022
 
1

Yes, you are correct. There's no ambiguity about the characters themselves, i.e. hello123 can't possibly anything else in UTF-8. The way you're slicing it, it doesn't matter whether you're slicing by character or by byte number.

So yes, this is safe, as long as your string is UTF-8 and thereby ASCII compatible.

See here for quick test: http://3v4l.org/XnM8s

Why this works:

The string "??hello123" in UTF-8 looks like this as bytes (I hope this aligns correctly):

e6 | bc | a2 | e5 | ad | 97 | 68 | 65 | 6c | 6c | 6f | 31 | 32 | 33
     ?      |      ?      | h  | e  | l  | l  | o  | 1  | 2  | 3

strpos will look for the byte sequence 68656c6c6f313233, returning 6 as the starting byte of "hello123". substr will slice 6 bytes from byte 0, returning "??". There is no ambiguity. You're finding and slicing by bytes, it doesn't matter how many characters there are.

You need to either work entirely in characters, in which case the string functions must be encoding aware. Or you work entirely in bytes, in which case the only requirement is that bytes aren't ambiguous (say "hello123" could match "??" encoded in BIG5, because the bytes are the same (they don't, just an example)). UTF-8 is self-synchronizing, meaning there's no such ambiguity.

Saturday, November 5, 2022
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :