PHP中如何高亮显示一个字符串在另一个字符串中的部分。比如一个字符串为:北京欢迎你!,另一个字符串为:北京。那么高亮显示后的结果为:北京欢迎你!

解决方案 »

  1.   

    $str = '北京欢迎你!';
    $find= '北京';
    $color='red';
    str_replace($find, "<font color={$color}>{$find}</font>", $str);
      

  2.   


    <?php
    $str="北京欢迎你";
    $find="北京";
    $replace="<span style=\"color:red\">".$find."</span>";
    echo str_replace($find,$replace,$str);
    ?>
      

  3.   


    <html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body></body>
    </html>
    <?php
        $str="北京欢迎你";
        $find="北京";
        $result=str_replace($find,"<font color='red'>$find</font>",$str);
        echo "$result";
    ?>