duanjingwei7239 2010-02-26 16:31
浏览 55
已采纳

突出文本中的搜索关键字

I am using this class to highlight the search keywords on a piece of text:

    class highlight
    {
        public $output_text;

        function __construct($text, $words)
        {
            $split_words = explode( " " , $words );
            foreach ($split_words as $word)
            {
                $text = preg_replace("|($word)|Ui" ,
                           "<font style=\"background-color:yellow;\"><b>$1</b></font>" , $text );
            }
            $this->output_text = $text;
        }
    }

If $text = "Khalil, M., Paas, F., Johnson, T.E., Su, Y.K., and Payer, A.F. (2008.) Effects of Instructional Strategies Using Cross Sections on the Recognition of Anatomical Structures in Correlated CT and MR Images. <i>Anatomical Sciences Education, 1(2)</i>, 75-83 "

which already contains HTML tags, and some of my search keywords are

$words = "Effects color"

The first look will highlight the word Effects, with <font style="background-color:yellow">Effect</font>, but the second loop will highlight the word color in the HTML tag. What should I do?

Is it possible to tell preg_replace to only highlight text when its not inside an alligator bracket?

  • 写回答

4条回答 默认 最新

  • douzhi4056 2010-02-26 16:34
    关注

    Use a HTML parser to make sure that you only search through text.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化