本文整理汇总了PHP中Nette\String::replace方法的典型用法代码示例。如果您正苦于以下问题:PHP String::replace方法的具体用法?PHP String::replace怎么用?PHP String::replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nette\String
的用法示例。
在下文中一共展示了String::replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: texyElements
/**
* Processes <texy>...</texy> elements.
*
* @author David Grudl, Jan Tvrdík
* @param string
* @return string
*/
public static function texyElements($s)
{
$texy = self::$texy;
if ($texy === NULL) throw new \InvalidStateException(get_class($this) . '::$texy must be set.');
return Nette\String::replace($s, '#<texy>(.*?)</texy>#s', function ($m) use ($texy) {
$s = $m[1];
$singleLine = (strpos($s, "\n") === FALSE);
$s = trim($s, "\n");
$tabs = strspn($s, "\t");
if ($tabs) $s = Nette\String::replace($s, "#^\t{1,$tabs}#m", '');
$s = $texy->process($s, $singleLine);
return "{syntax double}$s{{/syntax}}";
}
);
}
示例2: __invoke
/**
* Processes <texy>...</texy> elements.
*
* @author David Grudl, Jan Tvrdík
* @param string
* @return string
*/
public function __invoke($s)
{
$texy = $this->texy;
$autoChangeSyntax = $this->autoChangeSyntax;
if ($texy === NULL) throw new \InvalidStateException(get_class($this) . '::$texy must be set.');
return Nette\String::replace($s, '#<texy>(.*?)</texy>#s', function ($m) use ($texy, $autoChangeSyntax) {
$s = $m[1];
$singleLine = (strpos($s, "\n") === FALSE);
$s = trim($s, "\r\n");
$tabs = strspn($s, "\t");
if ($tabs) $s = Nette\String::replace($s, "#^\t{1,$tabs}#m", '');
$s = $texy->process($s, $singleLine);
return ($autoChangeSyntax ? "{syntax double}$s{{/syntax}}" : $s);
}
);
}
示例3: process
/**
* @param string
* @param array
* @return array of [sql, params]
*/
public function process($sql, $params)
{
$this->params = $params;
$this->counter = 0;
$this->remaining = array();
$cmd = strtoupper(substr(ltrim($sql), 0, 6));
// detect array mode
$this->arrayMode = $cmd === 'INSERT' || $cmd === 'REPLAC' ? 'values' : 'assoc';
/*~
\'.*?\'|".*?"| ## string
:[a-zA-Z0-9_]+:| ## :substitution:
\? ## placeholder
~xs*/
$sql = Nette\String::replace($sql, '~\'.*?\'|".*?"|:[a-zA-Z0-9_]+:|\\?~s', array($this, 'callback'));
while ($this->counter < count($params)) {
$sql .= ' ' . $this->formatValue($params[$this->counter++]);
}
return array($sql, $this->remaining);
}
示例4: macroAttr
/**
* {attr ...}
*/
public function macroAttr($content)
{
return String::replace($content . ' ', '#\)\s+#', ')->');
}
示例5: fixContentTypeMeta
/**
*
* @param string $document
* @param string $charset
* @return string
*/
public static function fixContentTypeMeta($document, $charset = 'utf-8')
{
return String::replace($document, '~<meta([^>]+http-equiv\\s*=\\s*)["\']*Content-Type["\']*([^>]+content\\s*=\\s*["\'][^;]+;)[\\t ]*charset=[^"\']+(["\'][^>]*)>~i', '<meta\\1"Content-Type"\\2 charset=' . $charset . '\\3>');
}
示例6: texyElements
/**
* Process <texy>...</texy> elements.
* @param string
* @return string
*/
public static function texyElements($s)
{
return String::replace($s, '#<texy([^>]*)>(.*?)</texy>#s', function ($m) {
list(, $mAttrs, $mContent) = $m;
// parse attributes
$attrs = array();
if ($mAttrs) {
foreach (String::matchAll($mAttrs, '#([a-z0-9:-]+)\\s*(?:=\\s*(\'[^\']*\'|"[^"]*"|[^\'"\\s]+))?()#isu') as $m) {
$key = strtolower($m[1]);
$val = $m[2];
if ($val == NULL) {
$attrs[$key] = TRUE;
} elseif ($val[0] === '\'' || $val[0] === '"') {
$attrs[$key] = html_entity_decode(substr($val, 1, -1), ENT_QUOTES, 'UTF-8');
} else {
$attrs[$key] = html_entity_decode($val, ENT_QUOTES, 'UTF-8');
}
}
}
return TemplateFilters::$texy->process($m[2]);
});
}
示例7: setRequestHeaders
/**
* Formats and adds custom headers to the current request
*/
protected function setRequestHeaders($request)
{
$headers = array();
foreach ($this->headers as $key => $value) {
//fix HTTP_ACCEPT_CHARSET to Accept-Charset
$key = String::replace($key, array('~^HTTP_~i' => '', '~_~' => '-'));
$key = String::replace($key, array('~(?P<word>[a-z]+)~i'), function ($match) {
return ucfirst(strtolower(current($match)));
});
if ($key == 'Et') {
$key = 'ET';
}
$headers[] = (!is_int($key) ? $key . ': ' : '') . $value;
}
if (count($this->headers) > 0) {
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
}
return $headers;
}
示例8: indent
/**
* 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', function ($m) {
return strtr($m[0], " \t\r\n", "");
});
$s = String::indent($s, $level, $chars);
$s = strtr($s, "", " \t\r\n");
}
return $s;
}
示例9: createHttpRequest
/**
* Creates current HttpRequest object.
* @return HttpRequest
*/
public function createHttpRequest()
{
// DETECTS URI, base path and script path of the request.
$uri = new UriScript;
$uri->scheme = isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? '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 = preg_match('#^[-._a-z0-9]+$#', $pair[0]) ? $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 = '';
}
$requestUri = String::replace($requestUri, $this->uriFilters['uri']);
$tmp = explode('?', $requestUri, 2);
$uri->path = String::replace($tmp[0], $this->uriFilters['path']);
$uri->query = isset($tmp[1]) ? $tmp[1] : '';
// normalized uri
$uri->canonicalize();
$uri->path = String::fixEncoding($uri->path);
// detect script path
if (isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME']) && strncmp($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])) === 0) {
$script = '/' . ltrim(strtr(substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])), '\\', '/'), '/');
} elseif (isset($_SERVER['SCRIPT_NAME'])) {
$script = $_SERVER['SCRIPT_NAME'];
} else {
$script = '/';
}
if (strncasecmp($uri->path . '/', $script . '/', strlen($script) + 1) === 0) { // whole script in URL
$uri->scriptPath = substr($uri->path, 0, strlen($script));
} elseif (strncasecmp($uri->path, $script, strrpos($script, '/') + 1) === 0) { // directory part of script in URL
$uri->scriptPath = substr($uri->path, 0, strrpos($script, '/') + 1);
} else {
$uri->scriptPath = '/';
}
// GET, POST, COOKIE
$useFilter = (!in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags'));
parse_str($uri->query, $query);
if (!$query) {
$query = $useFilter ? filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) : (empty($_GET) ? array() : $_GET);
}
$post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? array() : $_POST);
$cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? array() : $_COOKIE);
$gpc = (bool) get_magic_quotes_gpc();
$old = error_reporting(error_reporting() ^ E_NOTICE);
// remove fucking quotes and check (and optionally convert) encoding
if ($gpc || $this->encoding) {
$utf = strcasecmp($this->encoding, 'UTF-8') === 0;
$list = array(& $query, & $post, & $cookies);
while (list($key, $val) = each($list)) {
foreach ($val as $k => $v) {
unset($list[$key][$k]);
if ($gpc) {
$k = stripslashes($k);
}
if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
//.........这里部分代码省略.........
示例10: buildText
/**
* Builds text content.
* @return void
*/
protected function buildText()
{
$text = $this->getBody();
if ($text instanceof Nette\Templates\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));
}
}
示例11: detectUri
/**
* 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();
}
示例12: indent
/**
* 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;
}
示例13: texyElements
/**
* Process <texy>...</texy> elements.
* @param string
* @return string
*/
public static function texyElements($s)
{
return String::replace($s, '#<texy([^>]*)>(.*?)</texy>#s', callback(__CLASS__, 'texyCb'));
}
示例14: dump
/**
* Prints out a syntax highlighted version of the SQL command.
*
* @author David Grudl
* @param $sql string|DibiResult
* @return string
*/
public static function dump($sql)
{
$keywords1 = 'CREATE\\s+TABLE|CREATE(?:\\s+UNIQUE)?\\s+INDEX|SELECT|UPDATE|INSERT(?:\\s+INTO)?|REPLACE(?:\\s+INTO)?|DELETE|FROM|WHERE|HAVING|GROUP\\s+BY|ORDER\\s+BY|LIMIT|SET|VALUES|LEFT\\s+JOIN|INNER\\s+JOIN|TRUNCATE';
$keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE|INTEGER|CLOB|VARCHAR|DATETIME|TIME|DATE|INT|SMALLINT|BIGINT|BOOL|BOOLEAN|DECIMAL|FLOAT|TEXT|VARCHAR|DEFAULT|AUTOINCREMENT|PRIMARY\\s+KEY';
// insert new lines
$sql = " {$sql} ";
$sql = String::replace($sql, "#(?<=[\\s,(])({$keywords1})(?=[\\s,)])#", "\n\$1");
if (strpos($sql, "CREATE TABLE") !== FALSE) {
$sql = String::replace($sql, "#,\\s+#i", ", \n");
}
// reduce spaces
$sql = String::replace($sql, '#[ \\t]{2,}#', " ");
$sql = wordwrap($sql, 100);
$sql = htmlSpecialChars($sql);
$sql = String::replace($sql, "#([ \t]*\r?\n){2,}#", "\n");
$sql = String::replace($sql, "#VARCHAR\\(#", "VARCHAR (");
// syntax highlight
$sql = String::replace($sql, "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])({$keywords1})(?=[\\s,)])|(?<=[\\s,(=])({$keywords2})(?=[\\s,)=])#s", function ($matches) {
if (!empty($matches[1])) {
// comment
return '<em style="color:gray">' . $matches[1] . '</em>';
}
if (!empty($matches[2])) {
// error
return '<strong style="color:red">' . $matches[2] . '</strong>';
}
if (!empty($matches[3])) {
// most important keywords
return '<strong style="color:blue">' . $matches[3] . '</strong>';
}
if (!empty($matches[4])) {
// other keywords
return '<strong style="color:green">' . $matches[4] . '</strong>';
}
});
$sql = trim($sql);
return '<pre class="dump">' . $sql . "</pre>\n";
}
示例15: formatArray
/**
* 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})";
}