Skip to content

Commit

Permalink
added 增加字符串截取、查找、获取长度的封装。
Browse files Browse the repository at this point in the history
  • Loading branch information
cmpan committed May 13, 2017
1 parent 4431ec8 commit dba6da6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 589 deletions.
12 changes: 2 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
{
"name" : "windwork/util",
"description" : "windwork util component,Windwork 工具组件",
"version" : "0.3.1",
"version" : "0.4.0",
"require" : {
"php" : ">=5.5.0",
"windwork/core" : "*",
"windwork/cache" : "*",
"windwork/db" : "*",
"windwork/logger" : "*",
"windwork/storage" : "*",
"windwork/template" : "*",
"windwork/mailer" : "*",
"windwork/web" : "*"
"windwork/core" : "*"
},
"license" : "MIT",
"autoload" : {
"files" : [ "lib/helper.php" ],
"psr-4" : {
"wf\\util\\" : "lib"
}
Expand Down
94 changes: 0 additions & 94 deletions lib/JSOutput.php

This file was deleted.

85 changes: 85 additions & 0 deletions lib/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,89 @@ public static function safeString($str) {
public static function isEqual($str, $expect) {
return strtolower((string)$str) === strtolower((string)$expect);
}

/**
* 转换编码
* @param string $str
* @param string $from
* @param string $to
*/
public static function convertEncoding($str, $from, $to)
{
if(function_exists('iconv')) {
return iconv($from, $to . '//ignore', $str);
} elseif(function_exists('mb_convert_encoding')) {
return mb_convert_encoding($str, $to, $from);
}

throw new \Exception('请先安装PHP iconv或mb_string扩展');
}

/**
* 获取字符串长度
* @param string $str
* @param string $encoding = 'UTF-8'
* @throws \Exception
*/
public static function strlen($str, $encoding = 'UTF-8') {
if(function_exists('iconv_strlen')) {
return iconv_strlen($str, $encoding);
} elseif(function_exists('mb_strlen')) {
return mb_strlen($str, $encoding);
}
throw new \Exception('请先安装PHP iconv或mb_string扩展');
}

/**
* 截取字符串
* @param string $str
* @param int $len
* @param int $start
* @param string $encoding = 'UTF-8'
* @throws \Exception
*/
public static function substr($str, $len, $start = 0, $encoding = 'UTF-8') {
if(function_exists('iconv_substr')) {
return iconv_substr($str, $start, $len, $encoding);
} elseif(function_exists('mb_substr')) {
return mb_substr($str, $start, $len, $encoding);
}
throw new \Exception('请先安装PHP iconv或mb_string扩展');
}

/**
* 查找字符串第一次出现的位置
* @param string $haystack
* @param string $needle
* @param int $start = 0 开始查找的位置
* @param string $encoding = 'UTF-8'
* @throws \Exception
* @return string
*/
public static function strpos($haystack, $needle, $start = 0, $encoding = 'UTF-8') {
if(function_exists('iconv_substr')) {
return iconv_strpos($haystack, $needle, $start, $encoding);
} elseif(function_exists('mb_substr')) {
return mb_strpos($haystack, $needle, $start, $encoding);
}
throw new \Exception('请先安装PHP iconv或mb_string扩展');
}

/**
* 查找字符串最后一次出现的位置
* @param string $haystack
* @param string $needle
* @param int $start = 0 开始查找的位置
* @param string $encoding = 'UTF-8'
* @throws \Exception
* @return string
*/
public static function strrpos($haystack, $needle, $start = 0, $encoding = 'UTF-8') {
if(function_exists('iconv_substr')) {
return iconv_strrpos($haystack, $needle, $start, $encoding);
} elseif(function_exists('mb_substr')) {
return mb_strrpos($haystack, $needle, $start, $encoding);
}
throw new \Exception('请先安装PHP iconv或mb_string扩展');
}
}
187 changes: 0 additions & 187 deletions lib/Utf8.php

This file was deleted.

Loading

0 comments on commit dba6da6

Please sign in to comment.