当前位置: 首页>>代码示例>>PHP>>正文


PHP CString::substr方法代码示例

本文整理汇总了PHP中CString::substr方法的典型用法代码示例。如果您正苦于以下问题:PHP CString::substr方法的具体用法?PHP CString::substr怎么用?PHP CString::substr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CString的用法示例。


在下文中一共展示了CString::substr方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: frameworkPath

 /**
  * Replaces any reference to one of the framework's special directories in a path with the directory's actual path
  * and returns the usable path.
  *
  * A framework's directory is referenced in a path by wrapping its ID into double curly braces, as in
  * "{{PHRED_PATH_TO_FRAMEWORK_ROOT}}", optionally with "/" after the reference.
  *
  * @param  string $path The path to the file or directory (can be absolute or relative).
  *
  * @return CUStringObject The usable path.
  */
 public static function frameworkPath($path)
 {
     assert('!isset($path) || is_cstring($path)', vs(isset($this), get_defined_vars()));
     if (!isset($path)) {
         return null;
     }
     // Replace every "{{EXAMPLE_PATH}}" in the path string with the value of "EXAMPLE_PATH" key from $GLOBALS
     // variable if such key exists in the variable.
     $modified = false;
     $path = CRegex::replaceWithCallback($path, "/\\{\\{\\w+\\}\\}/", function ($matches) use(&$modified) {
         $pathVarName = CString::substr($matches[0], 2, CString::length($matches[0]) - 4);
         if (isset($GLOBALS[$pathVarName])) {
             $modified = true;
             return $GLOBALS[$pathVarName] . "/";
         } else {
             assert('false', vs(isset($this), get_defined_vars()));
             return $matches[0];
         }
     });
     if ($modified) {
         $path = CRegex::replace($path, "/\\/{2,}/", "/");
     }
     return $path;
 }
开发者ID:nunodotferreira,项目名称:Phred,代码行数:35,代码来源:CFilePath.php

示例2: toCharCodeHex

 /**
  * Returns the code point of a specified character, as a hexadecimal string.
  *
  * The returned string is always four characters in length.
  *
  * For instance, "A" would return "0041".
  *
  * @param  string $char The character.
  *
  * @return string The Unicode code point of the character.
  */
 public static function toCharCodeHex($char)
 {
     assert('is_cstring($char)', vs(isset($this), get_defined_vars()));
     assert('self::length($char) == 1', vs(isset($this), get_defined_vars()));
     return CString::substr(self::toEscString($char), 2);
 }
开发者ID:nunodotferreira,项目名称:Phred,代码行数:17,代码来源:CUString.php

示例3: isValid

 /**
  * Determines if the URL in a specified string is valid.
  *
  * @param  string $url The URL string to be looked into.
  * @param  bool $ignoreProtocolAbsence **OPTIONAL. Default is** `false`. Tells whether the URL in the string may
  * still be considered valid even if it does not indicate any protocol.
  *
  * @return bool `true` if the URL in the string is valid, `false` otherwise.
  */
 public static function isValid($url, $ignoreProtocolAbsence = false)
 {
     assert('is_cstring($url) && is_bool($ignoreProtocolAbsence)', vs(isset($this), get_defined_vars()));
     $parsedUrl = parse_url($url);
     if (!is_cmap($parsedUrl)) {
         return false;
     }
     if ($ignoreProtocolAbsence && !CMap::hasKey($parsedUrl, "scheme")) {
         // No protocol seems to be specified, try with the default one.
         $url = self::DEFAULT_PROTOCOL . "://{$url}";
         $parsedUrl = parse_url($url);
         if (!is_cmap($parsedUrl)) {
             return false;
         }
         if (!CMap::hasKey($parsedUrl, "scheme")) {
             return false;
         }
     }
     if (is_cstring(filter_var($url, FILTER_VALIDATE_URL))) {
         return true;
     } else {
         if (CMap::hasKey($parsedUrl, "host")) {
             // The `filter_var` function could fail to recognize an IPv6 as the URL's host (enclosed in square
             // brackets), so, in case of a valid IPv6 being the host, replace it with an IPv4 and give the URL another
             // try.
             $host = $parsedUrl["host"];
             if (CRegex::find($host, "/^\\[.*\\]\\z/")) {
                 $host = CString::substr($host, 1, CString::length($host) - 2);
                 if (CIp::isValidV6($host)) {
                     // Should not influence the validity if the string is present anywhere else.
                     $url = CString::replace($url, "[{$host}]", "127.0.0.1");
                     if (is_cstring(filter_var($url, FILTER_VALIDATE_URL)) && is_cmap(parse_url($url))) {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
开发者ID:nunodotferreira,项目名称:Phred,代码行数:49,代码来源:CUrl.php

示例4: filter

 /**
  * Filters a string or a collection of strings according to the expected output type(s) and returns the output
  * value(s).
  *
  * @param  mixed $inputStringOrDecodedCollection The string to be filtered or the array or map containing the
  * strings to be filtered. If the parameter's value is a JSON-encoded string, the output value is going to be
  * either an array or map.
  * @param  reference $success **OUTPUT.** After the method is called, the value of this parameter tells whether
  * the filtering was successful.
  *
  * @return mixed The output value or a collection of values of the expected type(s) after having been put through
  * the filter.
  */
 public function filter($inputStringOrDecodedCollection, &$success)
 {
     assert('is_cstring($inputStringOrDecodedCollection) || is_collection($inputStringOrDecodedCollection)', vs(isset($this), get_defined_vars()));
     $success = true;
     if ($this->m_expectedType != self::CARRAY && $this->m_expectedType != self::CMAP) {
         // The expected output type is not a collection; the input value must be of string type.
         if (!is_cstring($inputStringOrDecodedCollection)) {
             $success = false;
             return oop_x($this->m_defaultValue);
         }
         $inputString = $inputStringOrDecodedCollection;
         if ($this->m_expectedType == self::BOOL || $this->m_expectedType == self::INT || $this->m_expectedType == self::FLOAT || $this->m_expectedType == self::EMAIL || $this->m_expectedType == self::URL || $this->m_expectedType == self::IP) {
             // Trim the input string on both sides from whitespace, including Unicode whitespace and control
             // characters.
             $trimmingSubjectRe = CUString::TRIMMING_AND_SPACING_NORM_SUBJECT_RE;
             $inputString = CRegex::remove($inputString, "/^({$trimmingSubjectRe})+|({$trimmingSubjectRe})+\\z/u");
         }
         // Pre-process the string for integer and floating-point types.
         $looksLikeHex;
         if ($this->m_expectedType == self::INT || $this->m_expectedType == self::FLOAT) {
             if (CString::startsWith($inputString, "+")) {
                 // Remove the plus sign.
                 $inputString = CString::substr($inputString, 1);
             }
             $looksLikeHex = CRegex::find($inputString, "/^-?0x/i");
             if ($this->m_allowLeadingZeros && !($this->m_expectedType == self::INT && $this->m_allowHex && $looksLikeHex)) {
                 // Remove any leading zeros (except for special cases).
                 $inputString = CRegex::replace($inputString, "/^(\\D*)0*(?!\\b)/", "\$1");
             }
             if ($this->m_allowComma) {
                 $inputString = CRegex::remove($inputString, "/,(?=\\d{3}\\b)/");
             }
         }
         // Validate and sanitize the value according to its expected type.
         if ($this->m_expectedType == self::BOOL) {
             if (!CRegex::find($inputString, "/^(1|true|yes|on|0|false|no|off)\\z/i")) {
                 $success = false;
                 return $this->m_defaultValue;
             }
             return CString::equals($inputString, "1") || CString::equalsCi($inputString, "true") || CString::equalsCi($inputString, "yes") || CString::equalsCi($inputString, "on");
         }
         if ($this->m_expectedType == self::INT) {
             $value;
             if (!($this->m_allowHex && $looksLikeHex)) {
                 // Regular.
                 if (!CRegex::find($inputString, "/^-?(?!0(?!\\b))\\d+\\z/")) {
                     $success = false;
                     return $this->m_defaultValue;
                 }
                 $value = CString::toInt($inputString);
             } else {
                 // Hex.
                 if (!CRegex::find($inputString, "/^-?0x[0-9A-F]+\\z/i")) {
                     $success = false;
                     return $this->m_defaultValue;
                 }
                 $value = CString::toIntFromHex($inputString);
             }
             if (isset($this->m_intValidMin) && $value < $this->m_intValidMin || isset($this->m_intValidMax) && $value > $this->m_intValidMax) {
                 $success = false;
                 return $this->m_defaultValue;
             }
             if (isset($this->m_intClampingMin) && $value < $this->m_intClampingMin) {
                 $value = $this->m_intClampingMin;
             }
             if (isset($this->m_intClampingMax) && $value > $this->m_intClampingMax) {
                 $value = $this->m_intClampingMax;
             }
             return $value;
         }
         if ($this->m_expectedType == self::FLOAT) {
             if (!CRegex::find($inputString, "/^-?(?!0(?!\\b))\\d*\\.?\\d+(e[\\-+]?\\d+)?\\z/i")) {
                 $success = false;
                 return $this->m_defaultValue;
             }
             $value = CString::toFloat($inputString);
             if (isset($this->m_floatValidMin) && $value < $this->m_floatValidMin || isset($this->m_floatValidMax) && $value > $this->m_floatValidMax) {
                 $success = false;
                 return $this->m_defaultValue;
             }
             if (isset($this->m_floatClampingMin) && $value < $this->m_floatClampingMin) {
                 $value = $this->m_floatClampingMin;
             }
             if (isset($this->m_floatClampingMax) && $value > $this->m_floatClampingMax) {
                 $value = $this->m_floatClampingMax;
             }
             return $value;
//.........这里部分代码省略.........
开发者ID:nunodotferreira,项目名称:Phred,代码行数:101,代码来源:CInputFilter.php


注:本文中的CString::substr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。