Php 如何从特定字符限制Laravel5.4中的字符串?
我使用自动完成创建搜索选项。因此,当我搜索某个字符串时,它会自动显示结果,并为结果中搜索的字符串着色 我使用str_limit()函数来限制我的长文本,但是如果搜索的字符串比我给出的限制长,我就看不到搜索到了什么 有没有办法从特定字符开始限制 这是我的密码: 显示结果的刀片文件:Php 如何从特定字符限制Laravel5.4中的字符串?,php,string,laravel,Php,String,Laravel,我使用自动完成创建搜索选项。因此,当我搜索某个字符串时,它会自动显示结果,并为结果中搜索的字符串着色 我使用str_limit()函数来限制我的长文本,但是如果搜索的字符串比我给出的限制长,我就看不到搜索到了什么 有没有办法从特定字符开始限制 这是我的密码: 显示结果的刀片文件: <style> .color_str_search { color: #ff0000; font-size: 14px; font-weight: bold;
<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>
.color\u str\u搜索{
颜色:#ff0000;
字体大小:14px;
字体大小:粗体;
}
@foreach($title作为$title)
-
{{$title->name}
{!!str_limit(str_replace($str,$str“,$title->notes),110,“…”)
@endforeach
使用strpos定位位置和substr获取子字符串
<?php
$str = "I love php";
$position = strpos($str,"p");
$sub_str = substr($str,$position,$limit);
echo $sub_str;
?>
答案看起来像“ph”
希望这对您有所帮助。您可以使用一个php函数substr()
substr($str, start_from, length);
start_from-从字符串中的指定位置开始。起始是一个数字。字符串中的第一个字母以0开头
length—从start参数返回的长度
例如:substr($str,0,10)代码>您可以在php中使用substr()函数