【问题标题】:getting the code executed error while trying to highlight some words in php尝试突出显示php中的某些单词时出现代码执行错误
【发布时间】:2020-07-16 19:58:06
【问题描述】:

我尝试同时执行一些单词以突出显示这些单词。但问题是有时但并非所有时间都在执行代码。 我使用的代码在这里:

function highlightKeywords($text, $keyword) {
  
    $pos = strpos($text, $keyword);

    $wordsAry = explode(" ", $keyword);
    
    $wordsCount = count($wordsAry);
        
    for ($i = 0; $i < $wordsCount; $i++) {
        if ($pos === false) {
            if ($i === 0) {
                $highlighted_text = "<span style='font-weight:700;color:#151313;'>" . ucfirst(strtolower($wordsAry[$i])) . "</span>";
            } else {
                $highlighted_text = "<span style='font-weight:700;color:#151313;'>" . strtolower($wordsAry[$i]) . "</span>";
            }
        } else {
             if ($i === 0) {
                 $highlighted_text = "<span style='font-weight:700;color:#151313;'>" . strtolower($wordsAry[$i]) . "</span>";
             } else {
                $highlighted_text = "<span style='font-weight:700;color:#151313;'>" . $wordsAry[$i] . "</span>";
            }
        }
        $text = str_ireplace($wordsAry[$i], $highlighted_text, $text);
    }
    return $text;
}

这里的 $text 是我传递的整个文本,$keyword 是我想要突出显示的一个或多个单词。这里我对通过提问 make first letter caps in php but ucfirst(strtolower('string')) does not work 制作的第一个字母大写有些怀疑。但是从这个解决方案中,只有我重新排列了上面的代码。但我面临的问题是,当我通过$text='Vegetarische cup a Soup'$keyword='cup a soup' 时,我得到以下代码正在执行:

Vegetarische an style='font-weight:700;color:#151313;'>cupan> a Soup

谁能帮我解决这个错误。

【问题讨论】:

  • @gpl 如果代码工作正常,那么为什么它会在两者之间执行代码而不是突出显示关键字?
  • @kerbh0lz 如果我们输入一个句子,那么该句子也希望被突出显示,如果它是一个单词,那么该单词应该被突出显示
  • @RiggsFolly 它不会产生错误。
  • @RiggsFolly 无法编辑它:/ 所以我删除了它
  • @gpl 我终于让它工作了。如果有人希望我可以指出它在哪里。

标签: php html function loops highlight


【解决方案1】:

这是我在tomelliott.com的代码中使用的答案

function highlightkeywords($str, $search) {
    //$highlightcolor = "#daa732";
    $occurrences = substr_count(strtolower($str), strtolower($search));
    $newstring = $str;
    $match = array();
 
    for ($i=0;$i<$occurrences;$i++) {
        $match[$i] = stripos($str, $search, $i);
        $match[$i] = substr($str, $match[$i], strlen($search));
        $newstring = str_replace($match[$i], '[#]'.$match[$i].'[@]', strip_tags($newstring));
    }
 
    $newstring = str_replace('[#]', '<span style="font-weight:700;color:#151313;">', $newstring);
    $newstring = str_replace('[@]', '</span>', $newstring);
    return $newstring;
 
}

在这里我可以转义 html 标签,并在 stackoverflow 中为我的其他问题提供答案,以保持区分大小写make first letter caps in php but ucfirst(strtolower('string')) does not work

【讨论】:

  • 条带化标签确实是一个很好的功能,但我们不需要将文本小写,因为我们可以进行不区分大小写的比较。它只会增加额外的开销。
【解决方案2】:

查看此功能。希望它会有所帮助。您可以根据需要更改 CSS。

<style>
    #keyword{
        font-weight: bold;
        color: red;
        background-color: yellow;
    }
</style>

<?php
    function highlightKeywords($keyword, $text) {
        $filteredKeyword = trim(strip_tags($keyword));
        $wordsAry = explode(" ", $text);
        $wordsCount = count($wordsAry);
        
        for ($i = 0; $i < $wordsCount; $i++)
            if(strcasecmp($filteredKeyword, $wordsAry[$i]) == 0)
                $wordsAry[$i] = "<span id=\"keyword\">" . $wordsAry[$i] . "</span>";
        
        return implode(" ", $wordsAry);
    }
?>

【讨论】:

  • 非常感谢您抽出宝贵的时间。我从tomelliott.com/php/…得到了所需的答案
  • 这里我要做的就是从函数中转义html标签。因为需要考虑这种情况
  • 大声笑,你为什么不提到它:P 你说你想突出显示文本。我想我至少应该得到一个赞成票,因为我回答了你的问题。顺便说一句,该代码似乎有点棘手,因为它混合了 CSS n PHP。我强烈建议你不要混合它们。
猜你喜欢
  • 2017-10-23
  • 2016-01-19
  • 2015-01-12
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多