【问题标题】:Automatic website tooltips from custom glossary?来自自定义词汇表的自动网站工具提示?
【发布时间】:2015-06-27 16:03:48
【问题描述】:

我有一个包含大量技术内容的 PHP 网站。我为网站上使用的一些比较晦涩的术语创建了一个词汇表。我希望在用户将鼠标悬停在这些术语之一上时显示工具提示(或提示气泡,无论它们被称为什么)。

我发现了大量的 jQuery 扩展,它们似乎可以满足我的需求,但它们需要通过将 span 标记设置为特定类来手动链接到每个术语实例。

我希望自动完成。我应该如何进行?

【问题讨论】:

    标签: php jquery tooltip


    【解决方案1】:

    我建议在服务器端进行此操作。当页面上有很多元素时,jQuery 插件会减慢页面速度。 你可能会得到类似的东西:

    $content = "<p>Lorem ajax ipsum</p>";
    
    $terms = array(
        'ajax' => 'Asynchronous JavaScript and XML',
    );
    
    foreach ($terms as $term => $explained) {
        $content = str_replace($term, '<acronym title="' . htmlspecialchars($explained) . '">' . $term . '</acronym>', $content);
    }
    

    【讨论】:

    • +1 用于服务器端解决方案。但是你仍然需要添加一些样式甚至是一个简单的 JS 函数来在悬停时显示工具提示。
    • 其实不行,浏览器会自动使用title属性在悬停时显示工具提示。
    【解决方案2】:

    我建立了sroes'的答案,但需要解决两个问题:

    • 部分词,即在定义“rap”时,您不想在“Those raptors”中替换它
    • 自我替换,即“歌曲”的定义中包含“说唱”一词,然后会再次替换它,从而导致定义之上的定义。

    此示例可能会通过正则表达式或其他方式来移除嵌套的 foreach 循环进行改进,欢迎提出建议 - 但请记住 stritr 中的串行替换。

    function add_glossary_tooltips($text) {
        global $glossary;
        if(empty($glossary))
            return $text;
        // Create array of replacements, using only individual
        // words surrounded by spaces or other punctuation
    
        $endings = array(
            '.',
            ' ',
            ',',
            '!',
            '-',
            '?',
            '&',
        );
        $beginnings = array(
            '-',
            ' ',
        );
        $replacements = array();
        foreach ($glossary as $entry) {
            $clean_defintion = htmlentities(strip_tags($entry['definition']), ENT_QUOTES);
            $replacement = "<abbr
                    class='tooltip'
                    title='".$clean_defintion."'
                >"
                .$entry['glossary_word']
                ."</abbr>";
            foreach ($endings as $ending) {
                foreach ($beginnings as $beginning) {
                    $replacements[$beginning.$entry['glossary_word'].$ending] = $beginning.$replacement.$ending;
                }
            }
        }
    
        $text = stritr($text, $replacements);
    
        return $text;
    }
    

    这是由自定义的不区分大小写的 strstr 支持的。 (不是我的作品)

    function stritr($string, $one = NULL, $two = NULL){
    /*
    stritr - case insensitive version of strtr
    Author: Alexander Peev
    Posted in PHP.NET
    */
        if(  is_string( $one )  ){
            $two = strval( $two );
            $one = substr(  $one, 0, min( strlen($one), strlen($two) )  );
            $two = substr(  $two, 0, min( strlen($one), strlen($two) )  );
            $product = strtr(  $string, ( strtoupper($one) . strtolower($one) ), ( $two . $two )  );
            return $product;
        }
        else if(  is_array( $one )  ){
            $pos1 = 0;
            $product = $string;
            while(  count( $one ) > 0  ){
                $positions = array();
                foreach(  $one as $from => $to  ){
                    if(   (  $pos2 = stripos( $product, $from, $pos1 )  ) === FALSE   ){
                        unset(  $one[ $from ]  );
                    }
                    else{
                        $positions[ $from ] = $pos2;
                    }
                }
                if(  count( $one ) <= 0  )break;
                $winner = min( $positions );
                $key = array_search(  $winner, $positions  );
                $product = (   substr(  $product, 0, $winner  ) . $one[$key] . substr(  $product, ( $winner + strlen($key) )  )   );
                $pos1 = (  $winner + strlen( $one[$key] )  );
            }
            return $product;
        }
        else{
            return $string;
        }
    }/* endfunction stritr */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-23
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      相关资源
      最近更新 更多