/** * Safe String. Remove no letter's, no number's and other's no allowed Signs. * * @param string $string String to safe * @return string */ public static function safe($string) { static $items; if (is_null($items)) { $letters[] = 'abcdefghijklmnopqrstuvwxyz'; $letters[] = 'áćéǵíḱĺḿńóṕŕśúǘẃýź'; $letters[] = 'äëḧïöẗüẅẍÿ'; $letters[] = 'åůẘẙ'; $letters[] = 'ǎčďěǧȟǐǰǩľňǒřšťǔǚž'; $letters[] = '.-_ '; $items = array(); foreach ($letters as $item) { $items = array_merge($items, parent::str_split($item), parent::str_split(parent::strtoupper($item))); } $items = array_merge($items, parent::str_split('0123456789')); } $string = parent::str_split($string); $safe = ''; foreach ($string as $item) { if (in_array($item, $items)) { $safe .= $item; } } return parent::trim($safe); }
/** * Translate a long text by Google, if it too long, will separate it.. * * @param string $text String to translate. * @param string $SourceLan Translate from this language, eg: 'zh-tw'. Empty will auto detect. * @param string $ResultLan Translate to this language, eg: 'en'. Empty will auto detect. * @param integer $separate Separate text by a number of words, batch translate them and re combine to return. * * @return string Translated text. */ public static function translate($text, $SourceLan = null, $ResultLan = null, $separate = 0) { // If text too big, separate it. if ($separate) { if (\JString::strlen($text) > $separate) { $text = \JString::str_split($text, $separate); } else { $text = array($text); } } else { $text = array($text); } $result = ''; // Do translate by google translate API. foreach ($text as $txt) { $result .= self::gTranslate($txt, $SourceLan, $ResultLan); } return $result; }
/** * @group String * @covers JString::str_split * @dataProvider str_splitData */ public function testStr_split($string, $split_length, $expect) { $actual = JString::str_split($string, $split_length); $this->assertEquals($expect, $actual); }
/** * Parse the content by censoring blacklisted words. * Borrowed from php-decoda. * * @param string $content * @return string */ public function censureWords($content) { $censored = $this->params->get('censored_words', ''); $censored = array_filter(array_map(array('JString', 'trim'), explode(',', $censored))); foreach ($censored as $word) { $letters = JString::str_split($word); $regex = ''; foreach ($letters as $letter) { $regex .= preg_quote($letter, '/') . '{1,}'; } $content = preg_replace_callback('/(^|\\s|\\n)?' . $regex . '(\\s|\\n|$)?/is', array($this, '_censorCallback'), $content); } return $content; }