CUBISTCODE

CUBISTCODE

Free PHP Syntax Highlight script

<?

    /**
     * Copyright (C) 2003 Eric Bodden
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * as published by the Free Software Foundation; either version 2
     * of the License, or (at your option) any later version.
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     *
     * Highlights the searchwords and in the string with the given color.
     * Also takes care that no replacement inside HTML tags is made.
     * @param $search_words array of words to highlight
     * @param $string String to parse
     * @param $bgcolor color or array of colors to highlight with
     *
     * Changelog:
     * V1.0 Initial Version
     * V1.1 Added capability of highlighting in multiple colors.
     *      Thanks to Richard Danby [rdanby@cinoan.com].
     *
     * Example one: applying to a file with default color:
     *  $fd = fopen ($page, "r");
     *  $size = filesize($page);
     *  $html_text = fread ($fd, $size);
     *  $keywords = explode(" ",$query);
     *  print highlight_search($keywords,$html_text);
     *
     * Example two: applying to a string with array of colors:
     *  query="this test what";
     *  $keywords = explode(" ",$query);
     *  $colors[0]='#FF66CC';
     *  $colors[1]='#FFFF00';
     *  $colors[2]='#66FFFF';
     *  $colors[3]='#99CC00';
     *  $colors[4]='#9999FF';
     *  $html_text='<html><body>Lets see if this little test does what I expect this to do.</html></body>';
     *  print highlight_search($keywords,$html_text,$colors);
     */
    function highlight_search($search_words,$string,$bgcolors='yellow')
    {
        if (is_array($bgcolors)) {
            $no_colors=count($bgcolors);
        else {
            $temp=$bgcolors;
            unset($bgcolors);
            $bgcolors[0]=$temp;
            $no_colors=1;
        }
        $word_no=0;
        foreach($search_words as $search_word)
        {
            $regex1 ">[^<]*(";
            $regex2 ")[^<]*<";
            preg_match_all("/".$regex1.$search_word.$regex2."/i"$string$matchesPREG_PATTERN_ORDER);
            foreach($matches[0as $match)
            {
              preg_match("/$search_word/i"$match$out);
              $case_sensitive_search_word $out[0];
              $newtext str_replace($case_sensitive_search_word,"<span style=\"background-color: ".$bgcolors[($word_no $no_colors)].";\">$case_sensitive_search_word</span>"$match);
              $string str_replace($match$newtext$string);
            }
            $word_no++;
        }
        return $string;
    }

?>



Tags: developer, file, php, scripts

Rate This Article:



Privacy Policy | Copyright/Trademark Notification