Bu anahtar kelime içeriyorsa, dize sözcüğü vurgulayın

3 Cevap

Bu anahtar kelime içeriyorsa, nasıl tüm kelimeyi menchion senaryoyu yazmak? example: anahtar kelime "eğlenceli", dize - kuş komik, sonuç - Kuş * komik. i aşağıdakileri yapın

     $str = "my bird is funny";
     $keyword = "fun";
     $str = preg_replace("/($keyword)/i","<b>$1</b>",$str);

ancak yalnızca anahtar menshions. benim kuş * fun * ny

3 Cevap

Bu deneyin:

preg_replace("/\w*?$keyword\w*/i", "<b>$0</b>", $str)

\w*? anahtar önce herhangi bir kelime karakterleri ile eşleşir (en azından mümkün olduğunca) ve \w* herhangi bir kelime karakterler anahtar sonra.

Ve ben anahtar kelimeyi kaçmak için preg_quote kullanmanızı öneririz:

preg_replace("/\w*?".preg_quote($keyword)."\w*/i", "<b>$0</b>", $str)

Unicode desteği için, u bayrağı kullanın ve \p{L} yerine \w:

preg_replace("/\p{L}*?".preg_quote($keyword)."\p{L}*/ui", "<b>$0</b>", $str)

Sen aşağıdakileri yapabilirsiniz:

 $str = preg_replace("/\b([a-z]*${keyword}[a-z]*)\b/i","<b>$1</b>",$str);

Örnek:

$str = "Its fun to be funny and unfunny";
$keyword = 'fun';
$str = preg_replace("/\b([a-z]*${keyword}[a-z]*)\b/i","<b>$1</b>",$str);
echo "$str"; // prints 'Its <b>fun</b> to be <b>funny</b> and <b>unfunny</b>'

Arama ve dize, metin, gövde ve paragraf sözcüğü vurgulayın:

<?php $body_text='This is simple code for highligh the word in a given body or text';  //this is the body of your page
$searh_letter = 'this';  //this is the string you want to search for
$result_body = do_Highlight($body_text,$searh_letter);    // this is the result with highlight of your search word
echo $result_body;            //for displaying the result
function do_Highlight($body_text,$searh_letter){     //function for highlight the word in body of your page or paragraph or string
     $length= strlen($body_text);    //this is length of your body
     $pos = strpos($body_text, $searh_letter);   // this will find the first occurance of your search text and give the position so that you can split text and highlight it
     $lword = strlen($searh_letter);    // this is the length of your search string so that you can add it to $pos and start with rest of your string
     $split_search = $pos+$lword; 
     $string0 = substr($body_text, 0, $pos); 
     $string1 = substr($body_text,$pos,$lword);
     $string2 = substr($body_text,$split_search,$length);
     $body = $string0."<font style='color:#FF0000; background-color:white;'>".$string1." </font> ".$string2;
     return $body;
    } ?>