Add to tinymce

Wednesday, March 17th, 2010

Because I will forget unless I save a copy here.
This is a very basic function for adding a string to tinymce (I use it to add images that are listed elsewhere on a page).

  1. // Adding content to the end of the tinymce editor
  2. function addToTiny(newContent){
  3.         var inst = tinyMCE;
  4.         inst.execCommand('mceFocus',false,'content');
  5.         inst.execCommand('mceInsertContent', true, newContent);
  6. }

Display clean javascript code for copying

// Adding content to the end of the tinymce editor
function addToTiny(newContent){
	var inst = tinyMCE;
	inst.execCommand('mceFocus',false,'content');
	inst.execCommand('mceInsertContent', true, newContent);
}

Is valid page

Wednesday, March 17th, 2010

A little function I use to double check the items in a friendly url to see if they only use valid characters.

  1. // Check if the page name is correct
  2. function isValidPage($page){
  3.         $safe = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '_', '.');
  4.         $pageCheck = preg_split('//', strtolower($page), -1, PREG_SPLIT_NO_EMPTY);
  5.  
  6.         // Make sure that the page name has only safe variables
  7.         foreach($pageCheck as $key => $val){
  8.                 if(!in_array($val, $safe)) return false;
  9.         }
  10.         return true;
  11. }

Display clean php code for copying

// Check if the page name is correct
function isValidPage($page){
	$safe = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '_', '.');
	$pageCheck = preg_split('//', strtolower($page), -1, PREG_SPLIT_NO_EMPTY);

	// Make sure that the page name has only safe variables
	foreach($pageCheck as $key => $val){
		if(!in_array($val, $safe)) return false;
	}
	return true;
}

Is date

Wednesday, March 17th, 2010

See if a string is actually a date (used for friendly urls mostly)

  1. // See if a string from the url is an actual date
  2. function isDate($var){
  3.         $time = strtotime(str_replace('-', ' ', $var));
  4.         if($time){
  5.                 if(strlen($var) == 8){
  6.                         return Array('month', $var, $time);
  7.                 }elseif(strlen($var) == 11){
  8.                         return Array('day', $var, $time);
  9.                 }
  10.         }
  11.         return false;
  12. }

Display clean php code for copying

// See if a string from the url is an actual date
function isDate($var){
	$time = strtotime(str_replace('-', ' ', $var));
	if($time){
		if(strlen($var) == 8){
			return Array('month', $var, $time);
		}elseif(strlen($var) == 11){
			return Array('day', $var, $time);
		}
	}
	return false;
}

Htaccess

Wednesday, March 17th, 2010

This is a very basic .htaccess file as I like to use it. It simply redirects any request to the index.php if it’s not an existing file and not an existing directory.

  1. <IfModule mod_rewrite.c>
  2.  
  3. RewriteEngine On
  4.  
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. RewriteRule ^(.*)/? /index.php?loc=$1
  8.  
  9. </IfModule>

Display clean text code for copying

<IfModule mod_rewrite.c>

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/? /index.php?loc=$1

</IfModule>

It works as follows:

checks to see if mod-rewrite is enabled

You can add Options +FollowSymlinks on the line before RewriteEngine On
It needs to be enabled for mod rewrite to actually work

RewriteEngine On
Start the rewrite engine

RewriteCond %{REQUEST_FILENAME} !-f
Is a condition and returns true if the requested location is not an existing file.

RewriteCond %{REQUEST_FILENAME} !-d
Is a condition and returns true if the location is not an existing directory.

RewriteRule ^(.*)/? /index.php?loc=$1
Does the actual redirection, basicly it takes everything from behind http://www.mysite.domain/ and puts it behind loc=
So inside your index.php you can get the url as $_GET['loc']
You can then use PHP to analyse what was requested.

</IfModule>
The end of the rewriting.

There are lots of more complicated things you can do, but this is what I need more often and it does only the thing I need… which is good.