How to highlight keywords using php:
<?php
// your string.
$a = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using \'Content here, content here\', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for \'lorem ipsum\' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>';
// list of keywords that need to be highlighted.
$keywords = array('old','and','from');
$colors = array(
"Indian Red" => "#CD5C5C",
"Light Coral" => "#F08080",
"Salmon" => "#FA8072",
"Dark Salmon" => "#E9967A",
"Light Salmon" => "#FFA07A",
"Crimson" => "#DC143C",
"Red" => "#FF0000",
"Fire Brick" => "#B22222",
"Dark Red" => "#8B0000",
"Pink" => "#FFC0CB",
"Light Pink" => "#FFB6C1",
"Hot Pink" => "#FF69B4",
"Deep Pink" => "#FF1493",
"Medium Violet Red" => "#C71585",
"Pale Violet Red" => "#DB7093",
"Light Salmon" => "#FFA07A",
"Coral" => "#FF7F50",
"Tomato" => "#FF6347",
"Orange Red" => "#FF4500",
"Dark Orange" => "#FF8C00",
"Orange" => "#FFA500",
"Gold" => "#FFD700",
"Yellow" => "#FFFF00",
"Light Yellow" => "#FFFFE0",
"Lemon Chiffon" => "#FFFACD",
"Light Goldenrod Yellow" => "#FAFAD2",
"Papaya Whip" => "#FFEFD5",
"Moccasin" => "#FFE4B5",
"Peach Puff" => "#FFDAB9",
"Pale Goldenrod" => "#EEE8AA",
"Khaki" => "#F0E68C",
"Dark Khaki" => "#BDB76B",
"Lavender" => "#E6E6FA",
"Thistle" => "#D8BFD8",
"Plum" => "#DDA0DD",
"Violet" => "#EE82EE",
"Orchid" => "#DA70D6",
"Fuchsia" => "#FF00FF",
"Magenta" => "#FF00FF",
"Medium Orchid" => "#BA55D3",
"Medium Purple" => "#9370DB",
"Blue Violet" => "#8A2BE2",
"Dark Violet" => "#9400D3",
"Dark Orchid" => "#9932CC",
"Dark Magenta" => "#8B008B",
"Purple" => "#800080",
"Indigo" => "#4B0082",
"Slate Blue" => "#6A5ACD",
"Dark Slate Blue" => "#483D8B",
"Green Yellow" => "#ADFF2F",
"Chartreuse" => "#7FFF00",
"Lawn Green" => "#7CFC00",
"Lime" => "#00FF00",
"Lime Green" => "#32CD32",
"Pale Green" => "#98FB98",
"Light Green" => "#90EE90",
"Medium Spring Green" => "#00FA9A",
"Spring Green" => "#00FF7F",
"Medium Sea Green" => "#3CB371",
"Sea Green" => "#2E8B57",
"Forest Green" => "#228B22",
"Green" => "#008000",
"Dark Green" => "#006400",
"Yellow Green" => "#9ACD32",
"Olive Drab" => "#6B8E23",
"Olive" => "#808000",
"Dark Olive Green" => "#556B2F",
"Medium Aquamarine" => "#66CDAA",
"Dark Sea Green" => "#8FBC8F",
"Light Sea Green" => "#20B2AA",
"Dark Cyan" => "#008B8B",
"Teal" => "#008080",
"Aqua" => "#00FFFF",
"Cyan" => "#00FFFF",
"Light Cyan" => "#E0FFFF",
"Pale Turquoise" => "#AFEEEE",
"Aquamarine" => "#7FFFD4",
"Turquoise" => "#40E0D0",
"Medium Turquoise" => "#48D1CC",
"Dark Turquoise" => "#00CED1",
"Cadet Blue" => "#5F9EA0",
"Steel Blue" => "#4682B4",
"Light Steel Blue" => "#B0C4DE",
"Powder Blue" => "#B0E0E6",
"Light Blue" => "#ADD8E6",
"Sky Blue" => "#87CEEB",
"Light Sky Blue" => "#87CEFA",
"Deep Sky Blue" => "#00BFFF",
"Dodger Blue" => "#1E90FF",
"Cornflower Blue" => "#6495ED",
"Medium Slate Blue" => "#7B68EE",
"Royal Blue" => "#4169E1",
"Blue" => "#0000FF",
"Medium Blue" => "#0000CD",
"Dark Blue" => "#00008B",
"Navy" => "#000080",
"Midnight Blue" => "#191970",
"Cornsilk" => "#FFF8DC",
"Blanched Almond" => "#FFEBCD",
"Bisque" => "#FFE4C4",
"Navajo White" => "#FFDEAD",
"Wheat" => "#F5DEB3",
"Burly Wood" => "#DEB887",
"Tan" => "#D2B48C",
"Rosy Brown" => "#BC8F8F",
"Sandy Brown" => "#F4A460",
"Goldenrod" => "#DAA520",
"Dark Goldenrod" => "#B8860B",
"Peru" => "#CD853F",
"Chocolate" => "#D2691E",
"Saddle Brown" => "#8B4513",
"Sienna" => "#A0522D",
"Brown" => "#A52A2A",
"Maroon" => "#800000",
"White" => "#FFFFFF",
"Snow" => "#FFFAFA",
"Honeydew" => "#F0FFF0",
"Mint Cream" => "#F5FFFA",
"Azure" => "#F0FFFF",
"Alice Blue" => "#F0F8FF",
"Ghost White" => "#F8F8FF",
"White Smoke" => "#F5F5F5",
"Seashell" => "#FFF5EE",
"Beige" => "#F5F5DC",
"Old Lace" => "#FDF5E6",
"Floral White" => "#FFFAF0",
"Ivory" => "#FFFFF0",
"Antique White" => "#FAEBD7",
"Linen" => "#FAF0E6",
"Lavender Blush" => "#FFF0F5",
"Misty Rose" => "#FFE4E1",
"Gainsboro" => "#DCDCDC",
"Light Grey" => "#D3D3D3",
"Silver" => "#C0C0C0",
"Dark Gray" => "#A9A9A9",
"Gray" => "#808080",
"Dim Gray" => "#696969",
"Light Slate Gray" => "#778899",
"Slate Gray" => "#708090",
"Dark Slate Gray" => "#2F4F4F",
"Black" => "#000000"
);
// iterate through the list.
foreach($keywords as $keyword) {
// replace keyword with <span style="color:$color">$keyword</span>
$color = $colors[array_rand($colors)];
$a = preg_replace("/\b($keyword)\b/i","<span style=\"color:$color\">$1</span>", $a);
}
echo $a;
Comments
cryptex_core@yahoo.com
Hi,
Add new comment