public function testReplace() { $string = new String("Test11"); $this->assertEquals("Test12", $string->replace("11", "12")); $string = new String("Test11"); $this->assertEquals("fest11", $string->replace("test", "fest", null, true)); }
public function replace(&$data, $oldDomain, $newDomain) { if (!empty($data)) { if (is_string($data)) { $data = parent::replace($data, $oldDomain, $newDomain); } elseif (is_object($data) || is_array($data)) { if (is_object($data)) { $objectData = get_object_vars($data); } elseif (is_array($data)) { $objectData = $data; } foreach ($objectData as $field => $value) { if (is_object($data)) { $data->{$field} = $this->replace($data->{$field}, $oldDomain, $newDomain); } elseif (is_array($data)) { $data[$field] = $this->replace($data[$field], $oldDomain, $newDomain); } } } elseif (is_integer($data)) { //Nothing } elseif (is_bool($data)) { //Nothing } else { ob_start(); var_dump($data); throw new \Exception('Unsupported operand type: ' . ob_get_clean()); } } return $data; }
public static function flush($group = null, $key = null) { if ($group === null) { File::removeAllFrom(self::$dir); } else { if ($key == null) { return File::removeAllFrom(self::$dir . '/' . String::replace('.', '/', $group)); } else { return File::remove(self::$dir . '/' . String::replace('.', '/', $group) . '/' . $key); } } }
/** * Detects uri, base path and script path of the request. * @return void */ protected function detectUri() { $uri = $this->uri = new UriScript(); $uri->scheme = $this->isSecured() ? 'https' : 'http'; $uri->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : ''; $uri->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : ''; // host & port if (isset($_SERVER['HTTP_HOST'])) { $pair = explode(':', $_SERVER['HTTP_HOST']); } elseif (isset($_SERVER['SERVER_NAME'])) { $pair = explode(':', $_SERVER['SERVER_NAME']); } else { $pair = array(''); } $uri->host = $pair[0]; if (isset($pair[1])) { $uri->port = (int) $pair[1]; } elseif (isset($_SERVER['SERVER_PORT'])) { $uri->port = (int) $_SERVER['SERVER_PORT']; } // path & query if (isset($_SERVER['REQUEST_URI'])) { // Apache, IIS 6.0 $requestUri = $_SERVER['REQUEST_URI']; } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 (PHP as CGI ?) $requestUri = $_SERVER['ORIG_PATH_INFO']; if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { $requestUri .= '?' . $_SERVER['QUERY_STRING']; } } else { $requestUri = ''; } $tmp = explode('?', $requestUri, 2); $this->originalUri = new Uri($uri); $this->originalUri->path = $tmp[0]; $this->originalUri->query = isset($tmp[1]) ? $tmp[1] : ''; $this->originalUri->freeze(); $requestUri = String::replace($requestUri, $this->uriFilter[0]); $tmp = explode('?', $requestUri, 2); $uri->path = String::replace($tmp[0], $this->uriFilter[PHP_URL_PATH]); $uri->path = String::fixEncoding($uri->path); $uri->query = isset($tmp[1]) ? $tmp[1] : ''; // normalized uri $uri->canonicalize(); // detect base URI-path - inspired by Zend Framework (c) Zend Technologies USA Inc. (http://www.zend.com), new BSD license $filename = isset($_SERVER['SCRIPT_FILENAME']) ? basename($_SERVER['SCRIPT_FILENAME']) : NULL; $scriptPath = ''; if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) { $scriptPath = rtrim($_SERVER['SCRIPT_NAME'], '/'); } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) { $scriptPath = $_SERVER['PHP_SELF']; } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) { $scriptPath = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility } elseif (isset($_SERVER['PHP_SELF'], $_SERVER['SCRIPT_FILENAME'])) { // Backtrack up the script_filename to find the portion matching php_self $path = $_SERVER['PHP_SELF']; $segs = explode('/', trim($_SERVER['SCRIPT_FILENAME'], '/')); $segs = array_reverse($segs); $index = 0; $last = count($segs); do { $seg = $segs[$index]; $scriptPath = '/' . $seg . $scriptPath; $index++; } while ($last > $index && FALSE !== ($pos = strpos($path, $scriptPath)) && 0 != $pos); } // Does the scriptPath have anything in common with the request_uri? if (strncmp($uri->path, $scriptPath, strlen($scriptPath)) === 0) { // whole $scriptPath in URL $uri->scriptPath = $scriptPath; } elseif (strncmp($uri->path, $scriptPath, strrpos($scriptPath, '/') + 1) === 0) { // directory portion of $scriptPath in URL $uri->scriptPath = substr($scriptPath, 0, strrpos($scriptPath, '/') + 1); } elseif (strpos($uri->path, basename($scriptPath)) === FALSE) { // no match whatsoever; set it blank $uri->scriptPath = '/'; } elseif (strlen($uri->path) >= strlen($scriptPath) && (FALSE !== ($pos = strpos($uri->path, $scriptPath)) && $pos !== 0)) { // If using mod_rewrite or ISAPI_Rewrite strip the script filename // out of scriptPath. $pos !== 0 makes sure it is not matching a value // from PATH_INFO or QUERY_STRING $uri->scriptPath = substr($uri->path, 0, $pos + strlen($scriptPath)); } else { $uri->scriptPath = $scriptPath; } $uri->freeze(); }
/** * @param string $pathSource_ * @param string $pathTarget_ * * @throws \Components\Io_Exception */ public static function directoryCopy($pathSource_, $pathTarget_) { if (false === @is_dir($pathSource_)) { throw new Io_Exception('io', 'Source path must be an accessible directory.'); } if (false === @is_dir($pathTarget_)) { if (@file_exists($pathTarget_)) { throw new Io_Exception('io', 'Target path must not be an existing file.'); } static::directoryCreate($pathTarget_); } if (false === @is_dir($pathTarget_)) { throw new Io_Exception('io', 'Failed to copy directory. Unable to create target directory.'); } $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($pathSource_, \RecursiveDirectoryIterator::SKIP_DOTS | \RecursiveDirectoryIterator::KEY_AS_PATHNAME), \RecursiveIteratorIterator::CHILD_FIRST); foreach ($iterator as $entryPath => $entryInfo) { if ($entryInfo->isFile()) { $subPath = String::replace($entryPath, "{$pathSource_}/", ''); if (false === @is_dir("{$pathTarget_}/" . dirname($subPath))) { static::directoryCreate("{$pathTarget_}/" . dirname($subPath)); } @copy($entryPath, "{$pathTarget_}/{$subPath}"); } } }
foreach ($a->split() as $key => $value) { echo "{$key}: {$value}\n"; } // regex too! // output: /* Number found: 9 Number found: 1000 */ $b = new String('This 9 word sentence is less than 1000 letters long.'); $b->match("/([0-9]+)/")->each(function ($value) { echo "Number found: {$value}\n"; }); // replaces all numbers with NUMBER. Third arg must be true because last arg is a regular expression // output: This NUMBER word sentence is less than NUMBER letters long. echo $b->replace("/([0-9]+)/", 'NUMBER', true) . "\n"; // not using regular expressions, and lowercase // output: this9wordsentenceislessthan1000letterslong. echo $b->replace(' ', '')->toLower() . "\n"; // something a bit more complex // strips the trailing 0's and 9's and splits it into an array, adds an element, reverse sorts it and turns it into an array // output: /* Array ( [8] => hi there! [7] => 8 [6] => 7 [5] => 6 [4] => 5 [3] => 4
/** * Builds text content. * @return void */ protected function buildText() { $text = $this->getBody(); if ($text instanceof ITemplate) { $text->mail = $this; $this->setBody($text->__toString(TRUE)); } elseif ($text == NULL && $this->html != NULL) { // intentionally == $text = String::replace($this->html, array('#<(style|script|head).*</\\1>#Uis' => '', '#<t[dh][ >]#i' => " \$0", '#[ \\t\\r\\n]+#' => ' ', '#<(/?p|/?h\\d|li|br|/tr)[ >]#i' => "\n\$0")); $text = html_entity_decode(strip_tags($text), ENT_QUOTES, 'UTF-8'); $this->setBody(trim($text)); } }
/** * {attr ...} */ public function macroAttr($content) { return String::replace($content . ' ', '#\\)\\s+#', ')->'); }
/** * Process <texy>...</texy> elements. * @param string * @return string */ public static function texyElements($s) { return String::replace($s, '#<texy([^>]*)>(.*?)</texy>#s', callback(__CLASS__, 'texyCb')); }
/** * Indents the HTML content from the left. * @param string UTF-8 encoding or 8-bit * @param int * @param string * @return string */ public static function indent($s, $level = 1, $chars = "\t") { if ($level >= 1) { $s = String::replace($s, '#<(textarea|pre).*?</\\1#si', callback(create_function('$m', 'return strtr($m[0], " \\t\\r\\n", "\\x1F\\x1E\\x1D\\x1A");'))); $s = String::indent($s, $level, $chars); $s = strtr($s, "", " \t\r\n"); } return $s; }
public function getRelativePath(Io_Path $path_) { if ($this->isFile()) { $directory = $this->getParent(); } else { $directory = $this; } if ($directory->isParentOf($path_)) { return Io::path(ltrim(String::replace($path_->m_path, str_pad($directory->m_path, 1, '/', STR_PAD_RIGHT), ''), '/')); } $level = 1; $segments = explode('/', $directory->m_path); $path = $path_->m_path; while (array_pop($segments)) { if (!($current = implode('/', $segments))) { $current = '/'; } if (0 === mb_strpos($path, $current)) { if ('/' === $current) { $subPath = $path; } else { $subPath = String::replace($path, $current, ''); } return str_repeat('../', $level) . ltrim($subPath, '/'); } $level++; } return $file_->getPath(); }
protected function detectUri() { $uri = $this->uri = new UriScript(); $uri->scheme = $this->isSecured() ? 'https' : 'http'; $uri->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : ''; $uri->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : ''; if (isset($_SERVER['HTTP_HOST'])) { $pair = explode(':', $_SERVER['HTTP_HOST']); } elseif (isset($_SERVER['SERVER_NAME'])) { $pair = explode(':', $_SERVER['SERVER_NAME']); } else { $pair = array(''); } $uri->host = $pair[0]; if (isset($pair[1])) { $uri->port = (int) $pair[1]; } elseif (isset($_SERVER['SERVER_PORT'])) { $uri->port = (int) $_SERVER['SERVER_PORT']; } if (isset($_SERVER['REQUEST_URI'])) { $requestUri = $_SERVER['REQUEST_URI']; } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { $requestUri = $_SERVER['ORIG_PATH_INFO']; if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') { $requestUri .= '?' . $_SERVER['QUERY_STRING']; } } else { $requestUri = ''; } $tmp = explode('?', $requestUri, 2); $this->originalUri = new Uri($uri); $this->originalUri->path = $tmp[0]; $this->originalUri->query = isset($tmp[1]) ? $tmp[1] : ''; $this->originalUri->freeze(); $requestUri = String::replace($requestUri, $this->uriFilter[0]); $tmp = explode('?', $requestUri, 2); $uri->path = String::replace($tmp[0], $this->uriFilter[PHP_URL_PATH]); $uri->path = String::fixEncoding($uri->path); $uri->query = isset($tmp[1]) ? $tmp[1] : ''; $uri->canonicalize(); $filename = isset($_SERVER['SCRIPT_FILENAME']) ? basename($_SERVER['SCRIPT_FILENAME']) : NULL; $scriptPath = ''; if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) { $scriptPath = rtrim($_SERVER['SCRIPT_NAME'], '/'); } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) { $scriptPath = $_SERVER['PHP_SELF']; } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) { $scriptPath = $_SERVER['ORIG_SCRIPT_NAME']; } elseif (isset($_SERVER['PHP_SELF'], $_SERVER['SCRIPT_FILENAME'])) { $path = $_SERVER['PHP_SELF']; $segs = explode('/', trim($_SERVER['SCRIPT_FILENAME'], '/')); $segs = array_reverse($segs); $index = 0; $last = count($segs); do { $seg = $segs[$index]; $scriptPath = '/' . $seg . $scriptPath; $index++; } while ($last > $index && FALSE !== ($pos = strpos($path, $scriptPath)) && 0 != $pos); } if (strncmp($uri->path, $scriptPath, strlen($scriptPath)) === 0) { $uri->scriptPath = $scriptPath; } elseif (strncmp($uri->path, $scriptPath, strrpos($scriptPath, '/') + 1) === 0) { $uri->scriptPath = substr($scriptPath, 0, strrpos($scriptPath, '/') + 1); } elseif (strpos($uri->path, basename($scriptPath)) === FALSE) { $uri->scriptPath = '/'; } elseif (strlen($uri->path) >= strlen($scriptPath) && (FALSE !== ($pos = strpos($uri->path, $scriptPath)) && $pos !== 0)) { $uri->scriptPath = substr($uri->path, 0, $pos + strlen($scriptPath)); } else { $uri->scriptPath = $scriptPath; } $uri->freeze(); }
public function replace() { $str = new String('www.m�ller.com'); $this->assertEquals(new String('m�ller'), $str->replace('www.')->replace('.com')); $this->assertEquals(new String('muller'), $str->replace('�', 'u')); }
/** * Formats parameters to PHP array. * @param string * @param string * @return string */ public static function formatArray($s, $prefix = '') { $s = String::replace(trim($s), '~ ' . self::RE_STRING . '| ## single or double quoted string (?<=[,=(]|=>|^)\\s*([a-z\\d_]+)(?=\\s*[,=)]|$) ## 1) symbol ~xi', callback(__CLASS__, 'cbArgs')); $s = String::replace($s, '#\\$(' . self::RE_IDENTIFIER . ')\\s*=>#', '"$1" =>'); return $s === '' ? '' : $prefix . "array({$s})"; }
public function testReplace() { $string = new String("hoge huga"); $this->assertTrue($string->replace("hoge", "foo")->equals("foo huga")); }