如何限制特定字符在laravel 5.4中的字符串?(How to limit string in laravel 5.4 from specific char?)

编程入门 行业动态 更新时间:2024-10-20 16:40:38
如何限制特定字符在laravel 5.4中的字符串?(How to limit string in laravel 5.4 from specific char?)

我使用自动完成功能创建搜索选项。 所以当我搜索一些字符串时,它会自动显示结果,并为结果中搜索到的字符串着色。

我使用str_limit()函数来限制我的长文本,但如果搜索的字符串更长,那么我给出的限制我没有看到我有什么搜索。

有什么办法从特定的字符开始限制吗?

这是我的代码:

显示结果的刀片文件:

<style> .color_str_search { color: #ff0000; font-size: 14px; font-weight: bold; } </style> <ul id="searchResults"> @foreach($titles as $title) <li onClick="selectTitle('{{ $title->name}}');"> <div class="title">{{ $title->name }}</div> <div class="desc">{!! str_limit(str_replace($str,"<span class='color_str_search'>$str</span>",$title->notes), 110,'...') !!} </div> </li> @endforeach </ul>

I create search option, with autocomplete. So when I search some string it shows the result automatic and also color the string searched in the result.

I used str_limit() function to limit my long text, but if the searched string is longer then the limit I gave I don't see what I have search.

Is there any way to start the limit from specific char?

Here is my code:

Blade file that shows the results:

<style> .color_str_search { color: #ff0000; font-size: 14px; font-weight: bold; } </style> <ul id="searchResults"> @foreach($titles as $title) <li onClick="selectTitle('{{ $title->name}}');"> <div class="title">{{ $title->name }}</div> <div class="desc">{!! str_limit(str_replace($str,"<span class='color_str_search'>$str</span>",$title->notes), 110,'...') !!} </div> </li> @endforeach </ul>

最满意答案

使用strpos定位位置和substr以获取子串

<?php $str = "I love php"; $position = strpos($str,"p"); $sub_str = substr($str,$position,$limit); echo $sub_str; ?>

答案看起来像“ph”

希望这可以帮助你。

use strpos to locate the position and substr to get substring

<?php $str = "I love php"; $position = strpos($str,"p"); $sub_str = substr($str,$position,$limit); echo $sub_str; ?>

Answer would look like "ph"

Hope this helps you.

更多推荐

本文发布于:2023-08-05 19:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   laravel   char   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!