| arpad | yes | 2012-02-23 06:34 |
| cataphract | yes | 2011-11-23 15:33 |
| derick | no | 2011-08-14 11:16 |
| felipe | yes | 2011-08-06 15:07 |
| gwynne | yes | 2011-08-11 02:54 |
| iliaa | no | 2011-08-08 16:08 |
| klaussilveira | yes | 2011-12-13 11:17 |
| laruence | yes | 2011-08-13 10:48 |
| levim | no | 2011-12-19 10:16 |
| pajoye | yes | 2011-08-05 21:24 |
| pierrick | yes | 2011-08-06 16:31 |
| rasmus | no | 2011-08-05 21:41 |
| rdohms | yes | 2011-08-15 15:39 |
| salathe | no | 2011-08-13 11:57 |
| stas | yes | 2012-02-26 08:31 |
| tyrael | yes | 2011-08-03 15:32 |
Copied from wiki.php.net, last synchronized Oct 13, 2026 11:53 UTC
strncmp doesn't supporting negative length:
<?php
if (strncmp("prefix_num", "num", -3) === 0) {
echo "they have same suffix\n";
}
?>
running the above script in PHP 5.3.6 will result:
warning: Length must be greater than or equal to 0 in /tmp/1.php
We need to write some codes like following one to make it works as expect:
<?php
if (strncmp(substr("prefix_num", -3, 3), "num", 3) === 0) {
echo "they have same suffix\n";
}
?>
base on feature request: https://bugs.php.net/bug.php?id=36944 , I wrote a patch to make strn(case)cmp supporting negative length,
after patched, following script:
<?php
if (strncmp("prefix_num", "num", -3) === 0) {
echo "they have same suffix\n";
}
?>
will work as expect.
and if the abs of the negative length is greater than any strlen of the first two parameters, then strn(case)cmp will work as strncmp in the reverse order with the abs value:
<?php
var_dump(strncmp("prefix_num", "num", -10));
var_dump(strncmp("mun_xiferp", "mun", 10));
/** output:
int(7)
int(7)
*/
?>
Vote: Do you want this feature in PHP 5.4 — yes: 11 · no: 5 (closed)