/** * Callback function for array_walk use. * * @param $string */ protected function trimCallback(&$string) { $string = UTF8::trim($string); }
/** * Convert a string to e.g.: "snake_case" * * @return Stringy Object with $str in snake_case */ public function snakeize() { $str = $this->str; $encoding = $this->encoding; $str = UTF8::normalize_whitespace($str); $str = str_replace('-', '_', $str); $str = preg_replace_callback('/([\\d|A-Z])/u', function ($matches) use($encoding) { $match = $matches[1]; $matchInt = (int) $match; if ("{$matchInt}" == $match) { return '_' . $match . '_'; } else { return '_' . UTF8::strtolower($match, $encoding); } }, $str); $str = preg_replace(array('/\\s+/', '/^\\s+|\\s+$/', '/_+/'), array('_', '', '_'), $str); $str = UTF8::trim($str, '_'); // trim leading & trailing "_" $str = UTF8::trim($str); // trim leading & trailing whitespace return static::create($str, $this->encoding); }