欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

各种自己写的函数 有大用

<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/18
 * Time: 10:48
 */


function _startWith($str, $needle = '')
{
    return strpos($str, $needle) === 0;
}

function is_current_start($currentpath, $path, $path1 = '', $class = 'current')
{

    if (_startWith($currentpath, $path)) {

        return $class;
    }
    if (!empty($path1) && _startWith($currentpath, $path1)) {
        return $class;
    }
    return '';

}

function is_current($currentpath, $path, $path1 = '', $class = 'current')
{

    if ($currentpath == $path) {

        return $class;
    }
    if ($currentpath == $path) {
        return $class;
    }
    if (!empty($path1) && ($currentpath == $path1)) {
        return $class;
    }
    return '';

}

function is_current_fullpath($currentpath, $path, $path1 = '', $class = 'current')
{
    $currentpath = substr($currentpath, 7);
    $currentpath = strchr($currentpath, '/');
    $currentpath = substr($currentpath, 1);

    if ($currentpath == $path) {

        return $class;
    }
    if ($currentpath == $path) {
        return $class;
    }
    if (!empty($path1) && ($currentpath == $path1)) {
        return $class;
    }
    return '';

}

function categoryTypeCn($category_type)
{
    $category_type_arr = array(
        'article' => '文章',
        'pic' => '图片',
        'pics' => '图集',
        'shiping' => '视频',
        'media' => '媒体报道'
    );
    return $category_type_arr[$category_type];

}

function categorysul($categorys)
{
    $html = '';
    if (!empty($categorys) && is_object($categorys)) {
        $html .= '<ul class="list-group">' . "\n";

        foreach ($categorys as $key => $category) {
            //$html .='<li class="list-group-item">'.'<a href="'.'/admin/pc/categorys/'.'">'.$category->name.'</a>'."\n";

            $html .= '<li class="list-group-item list-group-item-my clearfix"><span class="category-name-my">' . $category->name . "</span>\n";

            $html .= '<div class="pull-right pull-right-category-my">' . '<a href="' . '/admin21232f297a57a5a743894a0e4a8/youjianzhan/pc/categorys/' . $category->id . '/edit' . '">修改</a> ';
            //$html .= ' <a href="#">查看</a>';
//            $html .= '<form action="'.'/admin/pc/categorys/'.$category->id.'" method="POST" class="form-delete-my" onsubmit="return window.confirm(\'你真的要删除吗?\')"  >
//                                            <input name="_method" type="hidden" value="DELETE">
//                                            <input type="hidden" name="_token" value="'.csrf_token().'">
//                                            <button type="submit" class="btn btn-link btn-link-my">删除</button>
//                                        </form>';
            $html .= '<form action="' . '/admin21232f297a57a5a743894a0e4a8/youjianzhan/pc/categorys/' . $category->id . '" method="POST" class="form-delete-my" onsubmit="return ajaxIsCanDelete('.$category->id.');"  >
                                            <input name="_method" type="hidden" value="DELETE">
                                            <input type="hidden" name="_token" value="' . csrf_token() . '">
                                            <button type="submit" class="btn btn-link btn-link-my">删除</button>
                                        </form>';
            $html .= '</div>' . "\n";
            $list_article = $category->category_is_list ? '列表页' : '内容页';

            $html .= '<div class="pull-right pull-right-category-my pull-right-category-sort-pic-islist-my">' .
                '<table ><tr>' .
                // '<td  width="15%">'.(!empty($category->category_is_pc_front_show)?'显示':'隐藏').'</td>'.
                '<td  width="27%">
                <label class="checkbox-inline">
                <input type="checkbox"  name="category_is_pc_front_show_' . $category->id . '" id="category_is_pc_front_show_' . $category->id . '"
                 value="1" ' . (!empty($category->category_is_pc_front_show) ? 'checked' : '') . ' >pc显示
                </label>
               <label class="checkbox-inline">
                <input type="checkbox"  name="category_is_wap_front_show_' . $category->id . '" id="category_is_wap_front_show_' . $category->id . '"
                 value="1" ' . (!empty($category->category_is_wap_front_show) ? 'checked' : '') . ' >wap显示
                </label>
                </td>' .
                '<td width="5%">' . (!empty($category->category_is_jump) ? '是' : '否') . '</td>' .
                '<td width="10%"><input type="text" name="category_sort_' . $category->id . '" value="' . $category->category_sort . '"  class="form-control input-sm form-control-sort-my" ></td>' .
                '<td width="15%">' . $list_article . '</td>';
            $html .= '<td width="15%">' . $category->category_pic_original . '</td>';
            $html .= '<td width="17%">' . $category->category_picbig_original . '</td>';
            $html .= '<td width="11%">' . categoryTypeCn($category->category_type) . '</td>';


            $html .= '</tr></table></div>' . "\n";


            if (!empty($category->child) && !empty(objectToArray($category->child))) {

                $html .= categorysul($category->child);

            }
            $html .= '</li>' . "\n";
        }
        $html .= '</ul>' . "\n";
    }
    return $html;

}

function arrayToObject($e)
{
    if (gettype($e) != 'array') return;
    foreach ($e as $k => $v) {
        if (gettype($v) == 'array' || getType($v) == 'object')
            $e[$k] = (object)arrayToObject($v);
    }
    return (object)$e;
}

function objectToArray($e)
{
    $e = (array)$e;
    foreach ($e as $k => $v) {
        if (gettype($v) == 'resource') return;
        if (gettype($v) == 'object' || gettype($v) == 'array')
            $e[$k] = (array)objectToArray($v);
    }
    return $e;
}

//二维数组变一维,第一个参是二维数组,第二个是数组里的字段名
function twoArrayToOne($rows, $title = 'title')
{
    if (empty($rows)) {
        return array();
    }
    $newOneArr = array();
    foreach ($rows as $row) {
        $newOneArr[] = $row[$title];
    }
    return $newOneArr;
}

//去掉首尾的中英文空格
function removeCnEnSpace($str)
{
    return mb_ereg_replace('(^( | )+|( | )+$)', '', $str);

}

//称除回车tab键
function removeTab($str)
{
    return preg_replace('/\t+/', '', $str);
}

//去掉首尾&nbsp;&nbsp;&nbsp;&nbsp;
function removeNbsp($str)
{

    $str = preg_replace('/^(&nbsp;)*/', '', $str);

    return preg_replace('/(&nbsp;)*$/', '', $str);
}

function remove_space($str)
{
    $str = str_replace(' ', '', $str);
    $str = removeNbsp($str);
    $str = removeCnEnSpace($str);
    $str = removeTab($str);
    $str = trim($str);

    return $str;
}

function isnull($var)
{

    if (!empty($var)) {

        return false;
    } else {
        if ($var === '0') {
            return false;
        }
    }
    return true;
}

function getAllSelfAndSonIds($res, &$ids = array())
{
    foreach ($res as $key => $row) {
        $ids[] = $row['id'];
        if (!empty($row['child'] && is_array($row['child']))) {
            getAllSelfAndSonIds($row['child'], $ids);
        }
    }
    return $ids;
}

function setIdKey($res)
{
    $new_res = array();
    foreach ($res as $key => $row) {
        $new_res[$row['id']] = $row;
        if (is_array($row['child']) && !empty($row['child'])) {
            $row_child = setIdKey($row['child']);
        }
        if (is_array($row['child']) && !empty($row['child'])) {
            $new_res[$row['id']]['child'] = $row_child;
        }
    }
    return $new_res;
}

function getAllSelfAndSons($res, $id)
{

    $result = array();
    foreach ($res as $key => $row) {

        if ($id == $key) {
            $result[$key] = $row;
            return $result;
            //dump($key);
            //dump($result[$key]);
        }
        if (is_array($row['child']) && !empty($row['child'])) {
            $tmp_result = getAllSelfAndSons($row['child'], $id);
            if (!empty($tmp_result)) {
                $result = $tmp_result;
                break;
            }
        }
    }
    return $result;
}

function getMainDomain($domain)
{
    if (starts_with($domain, 'http://')) {
        $main_domain = substr($domain, 7); // $domain is now 'www.example.com'
    }
    $main_domain_arr = explode('.', $main_domain);
    return $main_domain_arr[count($main_domain_arr) - 2] . '.' . $main_domain_arr[count($main_domain_arr) - 1];

}

function accordion_class_in()
{
    return "in";
}

//移除二维数组的 某个键的一维数组
function twoarrayUnset($twoarray, $from_key, $from_value)
{
    foreach ($twoarray as $key => $onearray) {
        if ($onearray[$from_key] == $from_value) {
            unset($twoarray[$key]);
        }
    }
    return $twoarray;


}

function twoarrayGetOne($twoarray, $from_key, $from_value)
{
    foreach ($twoarray as $key => $onearray) {
        if ($onearray[$from_key] == $from_value) {
            return array($twoarray[$key]);
        }
    }


}

function curl_csrf_token($net_url)
{
// 初始化一个 cURL 对象
    $net_url = 'http://wangzhangaiban.com/login';
    $curl = curl_init();
// 设置你需要抓取的URL
    curl_setopt($curl, CURLOPT_URL, $net_url);
// 设置header 这里的值为0如果为1的话 就会 一些header值(通过chrome可以看到的header值)
    curl_setopt($curl, CURLOPT_HEADER, 0);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 运行cURL,请求网页
    $data = curl_exec($curl);
// 关闭URL请求
    curl_close($curl);
// 显示获得的数据

    preg_match('/<input(.*)name=["\']_token["\'](.*)value=["\'](.*)["\'](.*)>/isU', $data, $arr);
    return $arr[3];
}

//下面结果返回真 就是包含 第一个参数是整个串 第二个参数是部分串
function strexists($haystack, $needle)
{
    return !(strpos($haystack, $needle) === FALSE);//注意这里的"==="
}

//下面这个忽略大小写的包含 第一个参数是整个串 第二个参数是部分串
function istrexists($haystack, $needle)
{
    return !(stripos($haystack, $needle) === FALSE);//注意这里的"==="
}


//查找字符串 第几次出现的位置
//写一个新的stripos, $count参数表示第count次出现的问题
function newstripos($str, $find, $count, $offset=0)
{
    $pos = stripos($str, $find, $offset);
    $count--;
    if ($count > 0 && $pos !== FALSE)
    {
        $pos = newstripos($str, $find ,$count, $pos+1);
    }
    return $pos;
}
function getDomainByUrl($url)
{
    $domainPos = newstripos($url,'/',3);
    if(empty($domainPos)){
        $domain = $url;
    }else {
        $domain = substr($url,0,$domainPos);
    }

    return $domain;
}
?>

普通分类: