| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace Heanup\Frame\Helper;
- /**
- * 格式转换
- */
- class Html extends \Heanup\Frame\Helper
- {
- /**
- * @param null $msg
- * @param null $url
- * @param array $get
- * @param int $wait
- */
- public static function location($msg = null, $url = null,$isParent=FALSE, $get = Array(), $wait = 1)
- {
- $url = self::getSiteUrl($url, $get);
- $target=$isParent?".parent":"";
- $str = "<script type='text/javascript'>function flash(){window".$target.".location.href='$url';}setTimeout('flash()','" . intval($wait) * 500 . "');</script>";
- if (!is_string($msg)) {
- $msg = $msg ? "操作成功!" : "操作失败!";
- }
- echo $str . $msg . " 正在<a href='$url'>跳转中</a>.....";
- die();
- }
- /**
- * 取得当前页地址
- * @return mixed
- */
- public static function self()
- {
- return $_SERVER['REQUEST_URI'];
- }
- /**
- * 调整url自动生成规则
- * @param string $action
- * @param array $get
- * @param bool $mergeGet
- * @return string
- */
- public static function getSiteUrl($action = '', Array $get = array(), $mergeGet = false)
- {
- $url = '/' . trim($action, '/');
- if ($mergeGet) {
- $get = array_merge($_GET, $get);
- }
- if ($get) {
- $get = urldecode(http_build_query($get));
- }
- if ($get) {
- $url .= '?' . $get;
- }
- return $url;
- }
- /**
- * 链接生成器
- * @param $url
- * @param array $get
- * @param null $text
- * @param null $attributes
- * @return string
- */
- public static function anchor($url, Array $get = array(), $text = null, $attributes = null)
- {
- $url = '/' . trim($url, "/") . '/?' . http_build_query($get);
- $text = strlen($text) <= 0 ? $url : $text;
- $string = "<a href='$url' $attributes>$text</a>";
- return $string;
- }
- /**
- * 简易分页函数
- * @param string $url 跳转地址
- * @param int $totalPage 总页数
- * @param int $current 当前页
- * @param int $records 总记录数
- * @param int $displayCount 总共显示的页数
- * @return string
- */
- public static function pagination($url, $totalPage = 10, $current = 1, $records = 100, $displayCount = 10)
- {
- $current = ($current < $totalPage) ? $current : $totalPage;
- $current = ($current >= 1) ? $current : 1;
- $first = 1;
- $half = ceil($displayCount / 2);
- $end = $totalPage;
- $prev = $current - 1;
- $next = $current + 1;
- foreach ($_GET as $key => $value) {
- if($value === ''){
- unset($_GET[$key]);
- }
- }
- $linkSum = "<li><span class='sum'>总数:$records</span></li>";
- $linkFirst = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $first)), '«') . '</li>';
- $linkPrev = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $prev)), '‹') . '</li>';
- $linkNext = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $next)), '›') . '</li>';
- $linkEnd = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $end)), '»') . '</li>';
- if ($current <= $half) {
- $linkFirst = '';
- $last = $displayCount;
- if ($last > $end) {
- $last = $end;
- }
- if ($current == 1) {
- $linkPrev = '';
- }
- if ($current == $end) {
- $linkNext = '';
- }
- } elseif ($current > ($end - $half) AND $current <= $end) {
- $first = $current - $half + 1;
- $last = $end;
- $linkEnd = '';
- if ($current == $end) {
- $linkNext = '';
- }
- } else {
- $first = $current - $half + 1;
- $last = $current + $half;
- }
- $string = null;
- for ($i = $first; $i <= $last; $i++) {
- if ($i == $current) {
- $string .= '<li class="active">' . self::anchor($url, array_merge($_GET, array('page' => $i)), "$i", "class='active'") . '</li>';
- } else {
- $string .= '<li>' . self::anchor($url, array_merge($_GET, array('page' => $i)), "$i") . '</li>';
- }
- }
- return $linkSum . $linkFirst . $linkPrev . $string . $linkNext . $linkEnd;
- }
- /**
- * 下拉菜单生成器
- * @param $list
- * @param $key
- * @param $text
- * @param null $attributes
- * @return null|string
- */
- public static function loopOutputSelect($list, $key = '', $text = '', $attributes = null)
- {
- if (!is_array($list) OR empty($list)) {
- return null;
- }
- $string = null;
- foreach ($list as $k => $v) {
- $selected = null;
- if ($k == $key) {
- $selected = "selected='selected'";
- }
- $string .= "<option value='$k' $selected >$v</option>\n";
- }
- if (!empty($text)) {
- $string = "<select name='$text' id='$text' $attributes>" . $string . "</select>";
- }
- return $string;
- }
- /**
- * 单选按钮生成器
- * @param $list
- * @param $key
- * @param $text
- * @param null $attributes
- * @return null|string
- */
- public static function loopOutputRadio($list, $key = '', $text = '', $attributes = null)
- {
- if (!is_array($list) OR empty($list)) {
- return null;
- }
- $i = 0;
- $string = null;
- foreach ($list as $k => $v) {
- $checked = null;
- if ($k == $key) {
- $checked = "checked='checked'";
- }
- $string .= "<input type='radio' name='$text' value='$k' id='$text-$i' $checked $attributes /><label for='$text-$i'>$v</label>\n";
- $i++;
- }
- return $string;
- }
- /**
- * 多选框生成器
- * @param $list
- * @param string $key
- * @param string $text
- * @param string $attributes
- * @param string $separator
- * @return bool|string
- */
- public static function loopOutputCheckbox($list, $key = "", $text = '', $attributes = "", $separator = "\n")
- {
- if (!is_array($list) || empty($list)) {
- return false;
- }
- $string = '';
- if (!is_array($key)) {
- $key = array($key);
- }
- foreach ($list as $k => $v) {
- $string .= "<input type='checkbox' name='$text" . "[]' value='$k' ";
- $string .= in_array($k, $key) ? " checked ='checked'" : "";
- $string .= $attributes;
- $string .= " />$v" . $separator;
- }
- return $string;
- }
-
- /**
- * 获取强密码
- * @param int $length
- * @return string
- */
- public static function getStrongCode($length = 8)
- {
- $length = $length > 8 ? intval($length) : 8;
- $arr = array();
- $arr[] = self::getRandoms($length - 4);
- $arr[] = strtoupper(self::getRandoms(1, 'char'));
- $arr[] = strtolower(self::getRandoms(1, 'char'));
- $arr[] = mt_rand(0, 9);
- $special = '!@#$%^*()';
- $arr[] = $special{mt_rand(0, 11)};
- shuffle($arr);
- return implode('', $arr);
- }
- /**
- * 获取随机数
- *
- * @param int $length
- * @param string $type (可选)
- * @return string
- */
- public static function getRandoms($length = 4, $type = '')
- {
- $returnStr = '';
- $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
- if ($type == 'char') {
- $start = 10;
- $end = 61;
- } elseif ($type == 'num') {
- $start = 0;
- $end = 9;
- } else {
- $start = 0;
- $end = 61;
- }
- for ($i = 0; $i < $length; $i++) {
- $returnStr .= $pattern{mt_rand($start, $end)}; // 生成php随机数
- }
- return $returnStr;
- }
- }
|