我使用自动完成功能创建搜索选项。 所以当我搜索一些字符串时,它会自动显示结果,并为结果中搜索到的字符串着色。
我使用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.
更多推荐
发布评论