public function testEndsWith() { $this->assertTrue(StrUtil::endsWith('foobar', 'bar'), 'foobar ends with bar'); $this->assertTrue(StrUtil::endsWith('foobar', ''), 'foobar ends with empty string'); $this->assertTrue(StrUtil::endsWith('', ''), 'empty string ends with empty string'); $this->assertFalse(StrUtil::endsWith('foobar', 'baz'), 'foobar does not end with baz'); $this->assertFalse(StrUtil::endsWith('foobar', 'foo'), 'foobar does not end with foo'); $this->assertFalse(StrUtil::endsWith('foobar', 'xfoobar'), 'foobar does not end with xfoobar'); }
/** * Joins two or more path components. * If joining more than two path components, the result is * the same as calling two-argument joinPath successively. * Moreover, this function is associative, i.e. * joinPath('a','b','c') has the same effect as * joinPath(joinPath('a', 'b'), 'c') or joinPath('a', joinPath('b', 'c')) * * Path components of zero length are ignored * * @param string $a first path component * @param string $b second path component * @param string ... any other path components to join * @returns string all the paths joined using a directory separator */ public static function joinPath($a, $b) { $args = func_get_args(); $num_args = count($args); $shouldAddDS = true; // start with $a, omit trailing directory separator if ($a == DIRECTORY_SEPARATOR || $a == '') { $shouldAddDS = false; $path = $a; } else { if (StrUtil::endsWith($a, DIRECTORY_SEPARATOR)) { $path = substr($a, 0, strlen($a) - 1); } else { $path = $a; } } // add other components for ($i = 1; $i < $num_args; $i++) { $part = $args[$i]; // DIRECTORY_SEPARATOR or empty string is a special case if ($part == DIRECTORY_SEPARATOR) { continue; } // first extract range of part without leading or trailing DS if (StrUtil::startsWith($part, DIRECTORY_SEPARATOR)) { $start = 1; } else { $start = 0; } if (StrUtil::endsWith($part, DIRECTORY_SEPARATOR)) { $end = strlen($part) - 1; } else { $end = strlen($part); } // append a path component if ($shouldAddDS) { $path .= DIRECTORY_SEPARATOR; } $shouldAddDS = true; $path .= substr($part, $start, $end); } return $path; }