Php 如果前3个字符有效,则显示整个单词
我得到了一些函数(部分):Php 如果前3个字符有效,则显示整个单词,php,Php,我得到了一些函数(部分): foreach($keyword作为$key){ $keyword=substr($key,0,3); $para=preg_replace(“/\b([a-z]*${keyword}[a-z]*)\b/i”、“$1”、$para); } 现在我可以突出显示foreach循环的结果,当然只有前3个字符,如何突出显示整个单词,其中3个字符是有效的 我也试过: foreach ($paras as $para) { $keyword = explode(" ",
foreach($keyword作为$key){
$keyword=substr($key,0,3);
$para=preg_replace(“/\b([a-z]*${keyword}[a-z]*)\b/i”、“$1”、$para);
}
现在我可以突出显示foreach循环的结果,当然只有前3个字符,如何突出显示整个单词,其中3个字符是有效的
我也试过:
foreach ($paras as $para) {
$keyword = explode(" ", $keyword);
foreach ($keyword as $key) {
$keywordcheck = substr($key, 0, 3);
$paracheck = preg_replace("/\b([a-z]*${keywordcheck}[a-z]*)\b/i","<span style='color:#20a8d8'>$1</span>",$para);
if (isset($paracheck)) {
$para = preg_replace("/\b([a-z]*${keyword}[a-z]*)\b/i","<span style='color:#20a8d8'>$1</span>",$para);
}
}
}
foreach($paras为$para){
$keyword=分解(“,$keyword”);
foreach($keyword作为$key){
$keywordcheck=substr($key,0,3);
$paracheck=preg_replace(“/\b([a-z]*${keywordcheck}[a-z]*)\b/i”、“$1”、$para);
if(isset($paracheck)){
$para=preg_replace(“/\b([a-z]*${keyword}[a-z]*)\b/i”、“$1”、$para);
}
}
}
新代码:
foreach ($paras as $para) {
$keyword = explode(" ", $keyword);
foreach ($keyword as $key) {
$key = substr($key, 0, 3);
$para = preg_replace("/\b({$key}[a-z]*)\b/i","<span style='color:#20a8d8'>$1</span>", $para);
}
echo '<tr><td style="width:15%"></td>';
echo '<td>';
echo 'META TITLE: '.$para.'';
echo '</td></tr>';
}
foreach($paras$para){
$keyword=分解(“,$keyword”);
foreach($keyword作为$key){
$key=substr($key,0,3);
$para=preg_replace(“/\b({$key}[a-z]*)\b/i”,“$1”,“$para”);
}
回声';
回声';
回音“元标题:”.$para.”;
回声';
}
试试这个
$para = "Microsoft is company placed in USA";
$keyword = ['com'];
foreach ($keyword as $key) {
$kw = substr($key, 0, 3);
$para = preg_replace("/\b({$kw}[-a-z]*)\b/i","<span style='color:#20a8d8'>$1</span>", $para);
}
echo $para;
$para=“微软公司位于美国”;
$keyword=['com'];
foreach($keyword作为$key){
$kw=substr($key,0,3);
$para=preg_replace(“/\b({$kw}[-a-z]*)\b/i”、“$1”、$para);
}
echo$para;
您可以分解字符串并使用Substr检查foreach中每个单词的前三个。
我使用str_replace将单词替换为span标记中包含的单词
$str = "a life with fitness is boring but it may fit some people. Apples new iFit is not highlighted";
$arr = explode(" ", $str);
Foreach($arr as &$word){
If(Substr($word,0,3) == "peo"){
// If word has a . Or any other special character after the word handle it.
If(ctype_alpha(Substr($word,-1))){
$word = str_replace($word, "<span style='color:#20a8d8'>".$word."</span>", $word);
}Else{
$char = substr($word,-1); //if it has a dot this "saves" it
// Here I remove the dot, and insert it last (after span)
$word = str_replace($word, "<span style='color:#20a8d8'>".substr($word,0,-1)."</span>", $word) . $char;
}
}
}
Echo implode(" ", $arr);
$str=“健康的生活是无聊的,但它可能适合某些人。如果它没有突出显示,它将是全新的”;
$arr=爆炸(“,$str”);
Foreach($arr as&$word){
If(Substr($word,0,3)=“peo”){
//如果单词后面有.或任何其他特殊字符,请处理它。
If(ctype_alpha(Substr($word,-1))){
$word=str_替换($word,“$word.”,$word);
}否则{
$char=substr($word,-1);//如果它有一个点,则“保存”它
//在这里,我删除圆点,最后插入(在跨度之后)
$word=str_replace($word,“.”。substr($word,0,-1)。“,$word)。$char;
}
}
}
回波内爆(“,$arr);
在我看来,没有理由使用正则表达式,因为它不是一种真正的模式。这只是前三个字符
编辑以确保它不会突出显示人物中的
。什么是“有效”?对不起。。。。。“fit”可以工作,但我只需要在整个字符串中绘制一个单词,这会让我看到一个从左到右,从左到右的列表开始,fit在哪里可以显示一个示例文本以及应该突出显示的内容?示例测试:微软公司位于美国$keyword='com';突出显示:Company上面的模式与您的示例相符。但是如果你只需要1个匹配,那么在preg_replace(第四个参数)中添加limit 1。好的,但是现在highlite只会每隔一秒更新一次参数结果我的代码如果前3个字符是“peo”,那么你会得到“people”(最后是点)。另外,您还添加了一个额外的foreach,因为有一个关键字数组。好的,但是如何用什么替换“fit”?威奇变量?if(substr($word,0,3)=?{…@Talgat什么关键字数组?请看上面的评论,我问什么是有效的。@指挥官是的。这是正确的。你也可以使用$search=“fit”和if(substr($word,0,3)=$search)
@Andreas我的意思是这个数组$keyword=explode(“,$keyword);
$str = "a life with fitness is boring but it may fit some people. Apples new iFit is not highlighted";
$arr = explode(" ", $str);
Foreach($arr as &$word){
If(Substr($word,0,3) == "peo"){
// If word has a . Or any other special character after the word handle it.
If(ctype_alpha(Substr($word,-1))){
$word = str_replace($word, "<span style='color:#20a8d8'>".$word."</span>", $word);
}Else{
$char = substr($word,-1); //if it has a dot this "saves" it
// Here I remove the dot, and insert it last (after span)
$word = str_replace($word, "<span style='color:#20a8d8'>".substr($word,0,-1)."</span>", $word) . $char;
}
}
}
Echo implode(" ", $arr);