Bolt\Helpers\Str::endsWith PHP Method

endsWith() public static method

Returns whether the subjects ends with the search string.
public static endsWith ( string $subject, string $search, boolean $caseSensitive = true ) : boolean
$subject string
$search string
$caseSensitive boolean
return boolean
    public static function endsWith($subject, $search, $caseSensitive = true)
    {
        if ($caseSensitive) {
            $subject = strtolower($subject);
            $search = strtolower($search);
        }
        return $search === '' || substr($subject, -strlen($search)) === $search;
    }

Usage Example

Beispiel #1
0
 /**
  * Gather related (present) files.
  *
  * Matches: foo(_local)?\.*(.dist)?
  *
  * i.e., if we're editing config.yml, we also want to check for
  * config.yml.dist and config_local.yml
  *
  * @param FileInterface $file
  *
  * @return FileInterface[]
  */
 private function getRelatedFiles(FileInterface $file)
 {
     // Match foo(_local).*(.dist)
     $base = $file->getFilename();
     if (Str::endsWith($base, '.dist')) {
         $base = substr($base, 0, -5);
     }
     $ext = pathinfo($base, PATHINFO_EXTENSION);
     $base = Str::replaceLast(".{$ext}", '', $base);
     $base = Str::replaceLast('_local', '', $base);
     $dir = $file->getParent();
     $related = [];
     foreach ([".{$ext}", "_local.{$ext}", ".{$ext}.dist"] as $tail) {
         $f = $dir->getFile($base . $tail);
         if ($f->getFilename() !== $file->getFilename() && $f->exists()) {
             $related[] = $f;
         }
     }
     return $related;
 }