【问题标题】:Getting a substr of a {{ var }} in php在 php 中获取 {{ var }} 的子串
【发布时间】:2021-04-05 16:06:37
【问题描述】:

我正在填充一个 HTML 表格:

<table class="table table-bordered">
    <thead>
        <tr>
            <th>Id</th>
            <th>Event Title</th>
            <th>Event snippet</th>
            <th>Memory Title</th>
            <th>Created at</th>
            <th colspan="2">Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach($events as $e)
            <tr>
                <td>{{ $e->id }}</td>
                <td>{{ $e->eventname }}</td>
                **<td> substr( {{$e->description}}, 0, 24) </td>**

这会产生: 61 事件 40012345 substr(40012345, 0, 24) 60 事件 4001234 substr( 事件有很长的描述。列表应该显示一个 25 字符的子字符串。, 0, 24)

谢谢...Dan'l

【问题讨论】:

  • 你可以使用 laravel 的字符串限制辅助方法 {{ Str::limit($e-&gt;description, 25) }}

标签: php laravel


【解决方案1】:

重新组织你的 html/php:

<td>{{substr($e->description, 0, 24)}}</td>

你也可以使用 Laravel Helpers 来处理字符串相关的变量。 Read docs here.

【讨论】:

  • 效果很好。这条老海狗还在学习新把戏。
【解决方案2】:

使用 substr 函数显示更少的数据:

{{ substr($e->description, 0, 24) }}

有关 substr 的更多信息,请访问此链接: https://www.php.net/manual/en/function.substr.php

【讨论】:

    【解决方案3】:

    你可以使用 laravel 内置的 str 辅助方法

    {{ Str::limit($e->description, 24, '...') }}
    

    谢谢:)

    【讨论】:

    • 这只是给出了一个未知的函数错误。
    猜你喜欢
    • 2017-05-07
    • 1970-01-01
    • 2015-07-08
    • 2013-02-13
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多