Highlight a Substring

<?php
       $text='Would you be so kind to highlight css-tricks.com in this string?';
       $search='css-tricks.com';

       echo textHighlight($text,$search);

       //Performs a regex-texthighlight
       function textHighlight($text,$search,$highlightColor='#0000FF',$casesensitive=false)
       {
               $modifier=($casesensitive) ? 'i' : '';
               //quote search-string, cause preg_replace wouldn't work correctly if chars like $?. were in search-string
               
Avatar of Chris Coyier
Chris Coyier on

Detect IE5 or IE6

function getMSIE6() {
       $userAgent = strtolower($_SERVER["HTTP_USER_AGENT"]);
       if (ereg("msie 6", $userAgent) || ereg("msie 5", $userAgent)) {
               return true;
       }
       return false;
}
Avatar of Chris Coyier
Chris Coyier on

Append Site Overlay DIV

$(function() {

   var docHeight = $(document).height();

   $("body").append("<div id='overlay'></div>");

   $("#overlay")
      .height(docHeight)
      .css({
         'opacity' : 0.4,
         'position': 'absolute',
         'top': 0,
         'left': 0,
         'background-color': 'black',
         'width': '100%',
         'z-index': 5000
      });

});

Overlays entire site with a black tint, disabling all links and bringing …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Time Ago Function

This can be used for comments and other from of communication to tell the time ago instead of the exact time which might not be correct to some one in another time zone.

The function only uses unix time stamp …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

JavaScript Array Contains

Javascript objects are really nice, but sometimes they are missing some useful little functions/methods. The example above is with Arrays. It’s really nice to know whether or not an item is contained within your array. Well you can write a …

Avatar of Chris Coyier
Chris Coyier on

Change Graphics Based on Season

Function

<?php

function current_season() {
       // Locate the icons
       $icons = array(
               "spring" => "images/spring.png",
               "summer" => "images/summer.png",
               "autumn" => "images/autumn.png",
               "winter" => "images/winter.png"
       );

       // What is today's date - number
       $day = date("z");

       //  Days of spring
       $spring_starts 
Avatar of Chris Coyier
Chris Coyier on (Updated on )

Browser Specific Hacks

/***** Selector Hacks ******/

/* IE6 and below */
* html #uno  { color: red }
 
/* IE7 */
*:first-child+html #dos { color: red } 
 
/* IE7, FF, Saf, Opera  */
html>body #tres { color: red }
 
/* IE8, FF, 
Avatar of Chris Coyier
Chris Coyier on (Updated on )

Sliding Background Links

$('a', '#nav').hover(function() {
         if(!$(this).parent().hasClass('current')) {
                 $(this).stop().animate({
                         backgroundPosition: '(0 -75px)'
                 });
         }
 }, function() {
         if(!$(this).parent().hasClass('current')) {
                 $(this).stop().animate({
                         backgroundPosition: '(0 -0)'
                 });
         }
 });

Slides up and down the background image of a link when rolled over. Requires background position plugin

Avatar of Chris Coyier
Chris Coyier on

Email Address Validation

Simple

$email = '[email protected]';
$validation = filter_var($email, FILTER_VALIDATE_EMAIL);

if ( $validation ) $output = 'proper email address';
else $output = 'wrong email address';

echo $output;

Advanced

This function doesn’t only check if the format of the given email address is …

Avatar of Chris Coyier
Chris Coyier on (Updated on )

URL Validation

$url = 'http://example.com';
$validation = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);

if ( $validation ) $output = 'proper URL';
else $output = 'wrong URL';

echo $output;
Avatar of Chris Coyier
Chris Coyier on (Updated on )