Never been to TextSnippets 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


« Newer Snippets
Older Snippets »
10 total  XML / RSS feed 

Random Number

$randomnumber= rand(1,10);

Replace 1 and 10 with the range you want to pick a number between.

Display Current Year

Useful for example for automatic copyright notices:

&copy; Copyright 2004 - <?php echo date("Y") ?>

Date In The Future

 $nextmonth = mktime(0, 0, 0, date("m")+1, 1, date("Y"));
 echo date("F", $nextmonth);

Display In 2-Column Table

The below can be applied to MT, EE, etc, and will display the content in a 2 column table. The EE tags below are just an example, it will work equally well if you replace it with MT, Wordpress, etc, tags.

 $set_table="0"; ?>

<table cellpadding="5" border="0">
{exp:gallery:categories gallery="{gallery_name}"}
$fs_table = $set_table +1;
if ($set_table == "1") {echo"";} ?>

Insert other tags here.

 if ($set_table == "2") {echo"";  $set_table="0";} ?>

Get Today's Date & Time

$today = date("F j, Y");

Use PHP Inside EE Entries

You'll need the following plugins:
Allow EE Code:
Allow PHP:

Set text formatting for the field you want to use this on to "None" (Admin > Weblog Administration > Custom Weblog Fields).

Change your template by adding the Allow EE tags around the field:


Now you can use PHP inside your entries like this:

<p>Bla bla regular entry textp>

echo "This will be processed as PHP.";

Strip A String using explode

In this example, I need to strip the page's current URL to take off anything that follows a ? in that URL.

$fs_refer= $_SERVER ['REQUEST_URI'];

$fs_refer = explode("?", $fs_refer);
echo "$fs_refer[0] is now a URL without ?.
"; echo "$fs_refer[1] is the bit that used to follow the ?."; ?>

Trim a line of text to X characters

 $varshort = substr($var,0,25); echo "$varshort"; ?>

Clean Up Form Input

// trim spaces from the beginning and the end of the variable
$field_name = trim($field_name);

// strip all HTML from the variables
$field_name = strip_tags($field_name);

// change the case of the variable to capitalize (It Will Look Like This - handy for Name fields)
$field_name = ucwords(strtolower($field_name));

// change the case of the first character to uppercase, the rest to lowercase (It will look like this)
$field_name = ucfirst(strtolower($field_name));

//change the case of the variable to lowercase
$field_name = strtolower($field_name);

// change the case of the variable to uppercase
$field_name = strtoupper($field_name);

Trim/Remove Spaces

To remove multiple occurences of whitespace characters in a string an convert them all into single spaces, use this:

$text = preg_replace('/\s+/', ' ', $text);

Strip whitespace (or other characters) from the beginning and end of a string

$variable = trim($variable);
« Newer Snippets
Older Snippets »
10 total  XML / RSS feed