<?php
// https://github.com/sugarcrm/sugarcrm_dev/blob/ae189cfa4ed4edd6a4e1e0d9d1d5ec66f46a0b74/include/utils.php#L2082-L2090
function str_begin($str, $begin) {
return (substr($str, 0, strlen($begin)) == $begin);
}
function str_end($str, $end) {
return (substr($str, strlen($str) - strlen($end)) == $end);
}
// Let's test edge cases
$funcs = array(
'str_begin',
'str_end',
);
$tests = array(
// array(array($main, $part), $expected)
array(array("foo", "foo"), true),
array(array("foo", ""), true),
array(array("", "foo"), false),
array(array("", ""), true),
);
foreach ($funcs as $func) {
foreach ($tests as $test) {
list($args, $expected) = $test;
list($main, $part) = $args;
echo $func . '("' . $main . '", "' . $part . '"):' . PHP_EOL;
echo " - Expected: "; var_dump($expected);
echo " - Actual: "; var_dump($func($main, $part));
echo PHP_EOL;
}
echo PHP_EOL;
}
- Output for 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- str_begin("foo", "foo"):
- Expected: bool(true)
- Actual: bool(true)
str_begin("foo", ""):
- Expected: bool(true)
- Actual: bool(true)
str_begin("", "foo"):
- Expected: bool(false)
- Actual: bool(false)
str_begin("", ""):
- Expected: bool(true)
- Actual: bool(true)
str_end("foo", "foo"):
- Expected: bool(true)
- Actual: bool(true)
str_end("foo", ""):
- Expected: bool(true)
- Actual: bool(true)
str_end("", "foo"):
- Expected: bool(false)
- Actual: bool(false)
str_end("", ""):
- Expected: bool(true)
- Actual: bool(true)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
str_begin("foo", "foo"):
- Expected: bool(true)
- Actual: bool(true)
str_begin("foo", ""):
- Expected: bool(true)
- Actual: bool(true)
str_begin("", "foo"):
- Expected: bool(false)
- Actual: bool(false)
str_begin("", ""):
- Expected: bool(true)
- Actual: bool(true)
str_end("foo", "foo"):
- Expected: bool(true)
- Actual: bool(true)
str_end("foo", ""):
- Expected: bool(true)
- Actual: bool(true)
str_end("", "foo"):
- Expected: bool(false)
- Actual: bool(false)
str_end("", ""):
- Expected: bool(true)
- Actual: bool(true)
preferences:
188.43 ms | 403 KiB | 216 Q