Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Tracy Floyd http://www.coalescedesign.com

Text Cleaner

// This function will remove unwanted stuff from submitted text.
// usage: $var = text_cleaner($str);


function text_cleaner($text){
	$text=str_replace("\"",""",$text); // Get rid of curly quotation marks
	$text=str_replace("  "," ",$text); // Get rid of double spaces (not tabs)
	$text=str_replace("\r","</p>",$text); //Windows files do \r\n, this is for a file created in Windows
}

Character Chopper

// This function will limit by number of characters.
// usage: $var = char_chop($str(string), $string_length limit of chars, default: 30));


function char_chop($str, $string_length = 30) {
	$s = strlen($str);
	if($s > $string_length){
		$str = substr(0, $string_length, $str);
		$str .= '...';
	}
	return($str);
}

Word Chopper

// The following will limit a string to $max_words words
// usage: $var = word_chop($str(string), $max_words(limit of words, default: 15));


function word_chop($str, $max_words = 15) {
	$e = explode(' ', $str);
	$w = count($e);
	if($w > $max_words) {
		$str = '';
		for($i=0;$i<$max_words;$i++) {
			$str .= ' '.$e[$i];
		}
	$str .= '...';
	}
	return($str);
}

Make Title Case

// Run text throughthis to make it title case
// "from russia with love" becomes:
// "From Russia with love"


function title_case($title) {
  // Our array of 'small words' which shouldn't be capitalised if
  // they aren't the first word.  Add your own words to taste.
  $smallwordsarray = array(
    'of','a','the','and','an','or','nor','but','is','if','then','else','when',
    'at','from','by','on','off','for','in','out','over','to','into','with'
    );
  // Split the string into separate words
  $words = explode(' ', $title);
  foreach ($words as $key => $word)
  {
    // If this word is the first, or it's not one of our small words, capitalise it
    // with ucwords().
    if ($key == 0 or !in_array($word, $smallwordsarray))
      $words[$key] = ucwords(strtolower($word));
  }
  // Join the words back into a string
  $newtitle = implode(' ', $words);
  return $newtitle;
}

Make Sentence Case

// Run text through this to make it sentence case...
// "the pretty pink sandwich" becomes:
// "The pretty pink sandwich"


function sentence_case($s) {
   $str = strtolower($s);
   $cap = true;
   for($x = 0; $x < strlen($str); $x++){
       $letter = substr($str, $x, 1);
       if($letter == "." || $letter == "!" || $letter == "?"){
           $cap = true;
       }elseif($letter != " " && $cap == true){
           $letter = strtoupper($letter);
           $cap = false;
       }
       $ret .= $letter;
   }
   return $ret;
}

Proper Ending for Numbers (i.e. 2nd, 3rd, 8th)

// Run numbers through this to add proper endings (i.e. 2nd, 3rd, 8th)


 function nth_num($age, $small = 0) {
 	$last_char_age = substr("$age", -1);
 	switch($last_char_age) {
 		case '1' :
 			$th = 'st';
			break;
		case '2' :
			$th = 'nd';
			break;
		case '3' :
			$th = 'rd';
			break;
		default :
			$th = 'th';
			break;
	}
	if ($age > 10 && $age < 20) $th = 'th';
	if (0 == $small) $niceage = $age.$th;
	if (1 == $small) $niceage = $age."<sup>$th</sup>";
	return $niceage;
	}

Curly Quote Maker

// Run text through this function to make curly quotes


	function curl_me($curlme) {
	// This should take care of the single quotes
	$curlme = preg_replace("/'([dmst])([ .,?!\)\/<])/i","&#8217;$1$2",$curlme);
	$curlme = preg_replace("/'([lrv])([el])([ .,?!\)\/<])/i","&#8217;$1$2$3",$curlme);
	$curlme = preg_replace("/(?<!=)(\s+)'((?:[^ >])?(?:.*?)(?:[^=]))'(\s*[^>&])/Ss","$1&#8216;$2&#8217;$3",$curlme);

	// time for the doubles
	$curlme = preg_replace('/(?<!=)(\s+)"(?=[ >]])((?:.*?)(?:[^=])?)"(\s*[^>&])/Ss',"$1&#8220;$2&#8221;$3",$curlme);
	// multi-paragraph
	$curlme = preg_replace('/<p>"(.*)<\/p>/U',"<p>&#8220;$1</p>",$curlme);

	// not a quote, but whatever
	$curlme = str_replace('Ñ','&#8212;',$curlme);
	$curlme = str_replace('Ð','&#8211;',$curlme);
	return $curlme;
	}

Mask address bar so always set to yourdomain.com

// You can use frames to mask your address bar so it always shows www.yourdomain.com when viewing your pages. Use the frameset below in your default page (index.htm). In this example, the frameset will load home.htm and start your site from there. But the address bar will stay showing www.yourdomain.com.

<frameset rows="*">
<frame src="home.htm">
</frameset> 

Ensure your page is not opened in a frame

// If you want to make sure your website is not opened in a frame from some other website, put this JavaScript in the <head> of your html page:

<script type="text/javascript">
if (top.location != self.location) { top.location.href = self.location.href; }
</script>

Backup a single MySQL table

// Use this to take a single table backup, with elements in double quotes delimited with a comma:

while ($row = mysql_fetch_array($query,MYSQL_NUM)) $output .= "\"" . implode("\",\"",str_replace("\r\n"," ",$row)) . "\"\r\n"; echo $output; // or write $output to a file