• lararvel 常用字符串操作


    字符串操作

    公共字符串

      $str = "我是一个处理字符串的函数";
    
    • 1

    after 方法将返回字符串中指定值后的所有内容。如果字符串中不存在这个值,它将返回整个字符串

    $a = Str::of($str)->after("一个");
     return Result::success($a);
    
    • 1
    • 2

    结果

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "处理字符串的函数"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    afterLast 方法返回字符串中指定值最后一次出现后的所有内容。如果字符串中不存在这个值,它将返回整个字符串:

     $a = Str::of($str)->afterLast('串');
    
    • 1

    结果:

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "的函数"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    append 方法为字符串附加上指定的值:

    $a = Str::of($str)->append(",我在你后面跟着");
    
    • 1
    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "的函数"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ascii 方法尝试将字符串转换为 ASCII 值:

     $a = Str::of('ü')->ascii();
    
    • 1

    结果

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "u"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    basename 方法将返回指定字符串的结尾部分

     $a = Str::of($str)->basename();
    
    • 1

    结果

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "u"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果有必要,您亦可提供提供一个「扩展名」,将从尾部的组件中移除它。

    $string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
    
    // 'baz'
    
    • 1
    • 2
    • 3

    before 方法返回字符串中指定值之前的所有内容:

    $slice = Str::of('This is my name')->before('my name');
    
    // 'This is '
    
    • 1
    • 2
    • 3

    beforeLast 方法返回字符串中指定值最后一次出现前的所有内容

    $a = Str::of(‘This is my is Is name’)->beforeLast(‘is’);

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "This is my "
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    camel 方法将指定字符串转换为 驼峰式 表示方法

    $converted = Str::of('foo_bar')->camel();
    
    // fooBar
    
    • 1
    • 2
    • 3

    contains 方法判断指定字符串中是否包含另一指定字符串(区分大小写):

    $contains = Str::of('This is my name')->contains('my');
    
    // true
    
    • 1
    • 2
    • 3

    您亦可以传递数组的值的形式来判断指定字符串是否包含数组中的任一值:

    $contains = Str::of('This is my name')->contains(['my', 'foo']);
    
    // true
    
    • 1
    • 2
    • 3

    containsAll 方法用于判断指定字符串是否包含指定数组中的所有值:

    $containsAll = Str::of('This is my name')->containsAll(['my', 'name']);
    
    // true
    
    • 1
    • 2
    • 3

    dirname 方法用于返回指定字符串的父级目录部分:

    $string = Str::of('/foo/bar/baz')->dirname();
    
    // '/foo/bar'
    
    • 1
    • 2
    • 3

    您亦可指定您想要从字符串中删除多少个目录级别,该参数是可选的:

    $string = Str::of('/foo/bar/baz')->dirname(2);
    
    // '/foo'
    
    • 1
    • 2
    • 3

    endsWith 方法用于判断指定字符串是否以另一指定字符串结尾:

    $a = Str::of('This is my name')->endsWith('name');
    // true
    
    • 1
    • 2
    $a = Str::of('This is my name java')->endsWith('name');
    // false
    
    • 1
    • 2

    您亦可以传递数组的值的形式来判断指定字符串是否包含指定数组中的任一值:

     $a = Str::of('This is my name')->endsWith(['name', 'foo']);
    
    // true
    
    $a = Str::of('This is my name')->endsWith(['this', 'foo']);
    
    // false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    exactly 方法用于判断指定字符串是否与另一字符串完全匹配:

        $a = Str::of('Laravel')->exactly('Laravel');
    
    // true
    
    • 1
    • 2
    • 3
        $a = Str::of('Laravel1')->exactly('Laravel');
    
    // false
    
    • 1
    • 2
    • 3

    finish 方法用于判断指定字符串末尾是否有特定字符,若没有,则将其添加到字符串末尾

    $adjusted = Str::of('this/string')->finish('/');
    
    // this/string/
    
    $adjusted = Str::of('this/string/')->finish('/');
    
    // this/string/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    URL

    action 方法为指定的控制器的 action 生成一个 URL:

    $a = action([UserController::class, 'user']);
    
    • 1

    结果:

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "http://127.0.0.1:8000/api/user"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    您可以将路由参数作为第二个参数传递给这个方法:

    $a = action([UserController::class, 'user'],['id'=>1]);
    
    • 1

    结果:

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "http://127.0.0.1:8000/api/user?id=1"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    asset 函数使用当前的请求协议( HTTP 或 HTTPS )来为资产生成 URL:

     $a = asset('img/photo.jpg');
    
    • 1

    结果:

    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "http://127.0.0.1:8000/img/photo.jpg"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    您可以配置 .env 文件中的 ASSET_URL 变量来设置资源的 URL 主机。
    当您在诸如 Amazon S3 这样的外部服务上托管您的资产时,这将非常有用:

    // ASSET_URL=http://example.com/assets
    
    $url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg
    
    • 1
    • 2
    • 3

    route 函数为给定的路由名生成一个 URL:

    users 必须存在
    在这里插入图片描述

    $a = route('users');
    
    • 1
    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "http://127.0.0.1:8000/users"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    您可以将路由参数作为该函数的第二个参数传递给它

    $a = route('users', ['id' => 1]);
    
    • 1
    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "http://127.0.0.1:8000/users?id=1"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    默认情况下, route 函数将生成一个绝对的 URL 。如果您想要生成相对的 URL ,您可以将 false 作为该方法的第三个参数传递给它:

    $a = route('users', ['id' => 1],false);
    
    • 1
    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "/users?id=1"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    secure_asset 函数将使用 HTTPS 协议为资产生成一个 URL:

    $url = secure_asset('img/photo.jpg');
    
    • 1
    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "https://127.0.0.1:8000/img/photo.jpg"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    secure_url 函数将使用 HTTPS 为指定路径生成一个完整的 URL:

    $a = secure_url('user/profile', ['age'=>18,'name'=>"jake"]);
    
    $url = secure_url('user/profile');
    
    $url = secure_url('user/profile', [1]);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    url 函数将为指定路径生成一个完整的 URL :

    $url = url('user/profile');
    
    $url = url('user/profile', [1]);
    
    • 1
    • 2
    • 3

    如果没有指定路径,该方法将会返回一个 Illuminate\Routing\UrlGenerator 实例:

    http://127.0.0.1:8000/str/after?id=10

    获取当前url

    $current = url()->current();
    
    • 1
    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "http://127.0.0.1:8000/str/after"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    获取全部url 包括参数

    $full = url()->full();
    
    • 1
    {
        "success": true,
        "failed": false,
        "code": 0,
        "message": "成功",
        "data": "http://127.0.0.1:8000/str/after?id=10"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    获取上一步的URL

    $previous = url()->previous();
    
    • 1

    Laravel 与Vue 跨域

    1.需要建立一个中间件

    php artisan make:middleware Cors
    
    • 1

    在这里插入图片描述

    1. 在Cors.php 文件的handle里写入
       public function handle(Request $request, Closure $next)
        {
            $response = $next($request);
            $response->header('Access-Control-Allow-Origin', '*');
            $response->header('Access-Control-Allow-Headers', '*');
            $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
            $response->header('Access-Control-Allow-Credentials', 'false');
            return $response;
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这里需要用星号(*),不然可能没有效果

      $response->header('Access-Control-Allow-Headers', '*');
      // 不能用下面的
      $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept, multipart/form-data, application/json');
    
    • 1
    • 2
    • 3
    1. 需要在Kernel.php加入全局
    \App\Http\Middleware\Cors::class
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    DotNetCore环境离线CentOS安装
    generative-model [ From GAN to WGAN ]
    使用echarts实现立体-柱状图
    国际新闻|PostgreSQL 14.3、13.7、12.11、11.16 和 10.21 发布
    我的第一个Spring Boot应用实现步骤以及遇到的问题
    css
    数据结构:二叉树基础
    分布式天花板?阿里百万架构师的ZK+Dubbo笔记,颠覆认知
    使用html2canvas实现超出浏览器部分截图
    GraphX 图计算实践之模式匹配抽取特定子图
  • 原文地址:https://blog.csdn.net/xxpxxpoo8/article/details/126846789