BradTrupp.com - Little slices of my life and my projects BradTrupp.com -- Pages
BradTrupp.com -- Pages -- Page 4 - 2007/09/23 - 2007/11/17

Page 4 - 2007/09/23 - 2007/11/17

<< Older || Newer >>


One Line Template Engine (PHP) (2007/11/17)

A One Line Template Engine in PHP

This is a really clever example of using a single line of PHP to fill in a HTML template and output a web page.

I found the original in the forums at http://www.webmasterworld.com/.

The one line engine --

print preg_replace("/\{([^\{]{1,100}?)\}/e","$$1",file_get_contents("template.tpl"));

It works by finding any string starting with an opening brace and ending with a closing brace and substituting the php variable with the same name.

An example of the template file -- template.tpl -- as used for this example.

<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>{header}</h1>
{text}
</body>
</html>

This template has three variables defined -- {title}, {header}, and {text}.

Using the template engine in its simpliest form.

$title="The One Line Template Engine Example";
$header="See this Example Work.";
$text="Magically all the placeholders are replaced!";
print preg_replace("/\{([^\{]{1,100}?)\}/e","$$1",file_get_contents("template.tpl"));

Elegantly simple?


Bash - A Basic Greeting Program. (Bash) (2007/10/31)

This is a basic Bash shell program to greet you with short message based on the time of the day.

#!/bin/bash

# get current hour - uses 24 hour format ( 0 to 23 )
hour=$(date +"%H");

msg="Good Morning, $USER"
if [ $hour -ge 12 -a $hour -lt 18 ]; then 
   msg="Good Afternoon, $USER" 
fi
if [ $hour -ge 18 ]; then 
  msg="Good Evening, $USER" 
fi

# display msg
echo "$msg"

Save the script as greetings.sh.

To run it, you would open a terminal session, change to directory where you saved the scripts, and enter the command as shown below.

./greetings.sh

Depending on the time of day, you should be greeted with Good Morning, Good Afternoon, or Good Evening.

Date Tips

The first line to get the hour will seem a little strange.

hour=$(date +"%H");

What is happening is that you are running the date command with the optional format specifier. The format is "date [OPTION]... [+FORMAT]" where date is the command and "%H" means format the results as an hour in 24 hour format.

The result of the date command gets saved as the value of the variable hour.


Prevent Automatic Resizing of Images (Firefox) (2007/10/23)

Firefox, by default, resizes large images to fit in the browser window.

This is usually a good thing but there are a few of use who want to see the image in the correct size the first time.

You can test automatic resizing in action by going over to a popular site for wallpapers like InterfaceLift.com and looking at some large sized wallpapers.

Entering the New Setting

Start Firefox. Click on the address bar or press Ctrl-l to place the cursor on the address bar.

Enter about:config and press enter. The configuration settings page displays.

Find the browser.enable_automatic_image_resizing parameter and double-click on it to set it to false.

browser.enable_automatic_image_resizing          default          boolean          true

Note that the status field changes from default to user set and the line becomes bolded.

browser.enable_automatic_image_resizing          user set          boolean          false

Finally

Now restart Firefox and see your images in the original sizes.


Boot into Safe Mode (Windows XP) (2007/10/11)

Windows XP - Boot into Safe Mode

What is Safe Mode

Windows Safe Mode is an optional way to boot your Windows operating system to run administrative and diagnostic tasks.

In safe mode, the operating system loads only the bare minimum of software that is required for the operating system to work -- including very basic video drivers -- so your programs may look rather different from normal.

This mode of operating allows you to troubleshoot and run diagnostics on your computer - for example - to help detect and remove spyware and viruses.

Use the System Configuration Utility method

Close all open programs and processes.

Click Start. Select Run. Enter MSCONFIG in the text box and click OK.

The System Configuration Utility will appear, Select the BOOT.INI tab. Check the "/SAFEBOOT" option and then click OK.

Restart your computer when prompted.

You should be automatically booted up into safe mode.

When you are done with troubleshooting in safe mode, just repeat the process above but uncheck the "/SAFEBOOT" option instead. If you forget this final step, you will keep rebooting into safe mode over and over again.

Use the F8 Key

Shut down Windows, and then turn off the power.

Wait a few seconds and turn the power back on.

Start tapping the F8 key about once per second. The Windows Advanced Options Menu will appear.

Some computers display a "keyboard error" message if you begin tapping the F8 key too soon. just restart the computer and try again.

Select the safe mode option.

Press enter and the Windows should start up in safe mode.

When you are done, just shut down Windows as normal. The next time you boot, Windows should restart as normal.

The F8 key method also works for other versions of Windows including Windows Vista.


Bash - An Introduction to Scripting (Bash) (2007/10/10)

Bash is a Unix shell program and is the default shell for Ubuntu, as well as many Linux systems and Mac OS X.

A Unix shell, which is commonly called "the command line", provides the traditional user interface for the operating system. You use it by entering commands for the shell to execute, but can also write and execute complex shell programs to do most anything.

In Microsoft Windows, this would be equivalent to command.com (or cmd.exe) for the command prompt.

A Basic Bash Script

It is best to start your Bash script with the "shebang" line -- although it is somewhat optional being the default shell anyway.

#!/bin/bash

Note: The "shebang" (also called a hashbang, hashpling, or pound bang) refers to a pair of characters "#!" that, when used as the first two characters on the first line of a script, causes Unix-like operating systems to execute that script using the interpreter specified by the rest of that line.

It is also good practice to end your script with the exit command.

exit 0

Here is the standard over-used "Hello World" example you see in so many programming languages.

#!/bin/bash
echo "Hello World!"
exit 0

Enter these 3 lines into a file using the text editor of your choice and save it as "helloworld".

Bash Comments

Comment lines in Bash scripts are similar to other programming languages where a special character, in this case "#" (the hash or pound sign), is used to delimit the start of a comment line.

# This is a comment line

Make it Executable

Until you mark your script as executable, it is nothing more than a text file.

The easiest way is to use the chmod command to change the file permissions.

Open a terminal session, navigate to the directory that you saved "helloworld" into and enter the command:

chmod u+x ./helloworld

Running a Bash Script

Once again, open a terminal session, navigate to the directory that you saved "helloworld".

If you enter the command

helloworld

you will likely get an error like "command not found".

This is because the default search path for commands does not include the current directory -- so the shell can not find your program.

Now enter the command

./helloworld

and your script should run and display the expected "Hello World!" output.

The "./" means look in the current directory.

You can also fully qualify the script (assuming the script is in freddie's desktop)

/home/freddie/Desktop/helloworld

and again the script should run.

What's Next?

Now we are ready to write some useful scripts, but first here are some related web sites.


CSS - A Few Quick Tips (Website Tips) (2007/10/09)

Here are a few tips and tricks using CSS or Cascading Style Sheets to put a few special effects on your web pages.

Just copy and paste the sample code...

Roll-over Color Text Links

Your text links will change color as the mouse passes over. Insert this code into the <HEAD> of your document...

<style type="text/css">
<!--
A:hover {color:red}
-->
</style>

Links with no Underline

Put this code in the <HEAD> of your document to remove the underline from all links on your page...

<style type="text/css">
<!--
A:link {text-decoration:none}
A:visited {text-decoration:none}
-->
</style>

Or, remove the underline from individual links like this...

<a href="page.html" style="text-decoration: none">link</a>

Links with a Line Above and Below

This works well as a hover attribute on links, but can also be applied to all of your links. It will show the normal underline plus a line above the link too...

<style type="text/css">
<!--
A:hover {text-decoration:overline underline}
-->
</style>

Sample Link with a Line Above and Below

Highlighted Text

To highlight important text on your page or just to get words to stand out...

<span style="background-color:yellow">highlighted text</span>

Sample Highlighted Text

To highlight all links on your page, put this code in the <HEAD> of your document:

<style type="text/css">
<!--
A:hover {background-color: orange}
-->
</style>

Background Images

This will create a background image that doesn't repeat...

<style type="text/css">
<!--
BODY {background: #ffffff url(bg.gif) no-repeat}
-->
</style>

You can also center the image on the web page...

<style type="text/css">
<!--
BODY {background: #ffffff url(bg.gif) no-repeat center}
-->
</style>


Disable Blinking Elements (Firefox) (2007/10/05)

One of the things that annoys me the most on web pages other than scrolling marquee text is blinking text.

Yes. This text is... blinking

Fortunately, it is a simple change to the Firefox browser configuration to stop the silliness.

Entering the New Settings

Start Firefox.

Click on the address bar or press Ctrl-l to place the cursor on the address bar.

Enter about:config and press enter.

The configuration settings page displays.

Find the entry for browser.blink_allowed and change to false.

Fixed!

Make the Text Blink!

Two ways really --

Use the non-standard <blink> tag -- for example <link>Blink!<blink>

or

Use the css style --for example, <span style="text-decoration: blink;">blinking</span>

Now that you know how to make your text blink, please DON'T do it.

Thanks.


Restore your Database from a Backup (MySql) (2007/09/29)

Restore your Database from a Backup
By Brad Trupp (c) 2007

Some time ago, I did an short article on using MYSQLDUMP and CRON to backup databases.

Database backups are a good thing to have, but unfortunately someday you might just need do a restore from those backups.

Do You Know How?

The backups you created using the mysqldump utility earlier simply generate a text file full of SQL commands.

For example:

-- MySQL dump 10.9
--
-- Host: localhost Database: db4wordpress
-- ------------------------------------------------------
-- Server version 4.1.20-log

[...snip...]

DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL auto_increment,
`cat_name` varchar(80) NOT NULL default 'New Category',
`disp_position` int(10) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

[...snip...]

It becomes a simple matter of running MySQL from the terminal session command line and piping the SQL commands into it to recreate the database.

mysql -h hostname -u username -pmydbpassword databasename < mybackupfile.sql

For example:

mysql -h localhost -u root -pABCD1234 db4wordpress < backup_20071231.sql

Test Your Backups

Quoting those words from The Hitchhiker's Guide to the Galaxy -- Don't Panic.

It is a good idea to try a restore on a test database sometime long before you actually need to do one for real.


Nine Random Thoughts on Improving your Web Site. (Website Tips) (2007/09/26)

By Brad Trupp (c) 2007

Here are 9 random thoughts on improving your web site --

  1. Get a Unique Logo. Your logo is your brand and identifies you. People remember pictures.

  2. Create a Tag-line. Tell everyone what you are all about -- in 8 words or less.

  3. Define a Distinct Layout. Create a consistent site layout that gives the impression of organization, ease of use, and professionalism.

  4. Use Whitespace Liberally. Keeps your web site easy to read. Remember -- less is almost always more -- and don't fill in every square inch of extra space with advertisements.

  5. Design for Scanners, not Readers. People read differently on the web. Blocks of content should jump out and be easy to spot with a quick glance at each page.

  6. Use Stock Photos. Make your web site look as professional. Not very many of us are graphic artists. I'm not.

  7. Check your Content for Typos. Nothing looses people's trust in a web site faster than typos do.

  8. Contact Information. Have contact information and make it easy to find -- especially if you are selling something. No one likes to deal with an anonymous site.

  9. Page Titles. Do not underestimate the power of the simple TITLE tag on your web pages to search engines. Make it meaningful.

I hope you find this short list helpful.

Feel free to comment below if you are more suggestions on this topic.


Mac Mini HTPC - More Great Software (Apple) (2007/09/23)

Mac Mini HTPC - More Great Software
By Brad Trupp (c) 2007

The first 3 parts of this series covered the basics in getting a Mac Mini hooked up and able to play all types of media.

There are a number of other features I may get into at a later time, such as TV tuners that plug into the USB port to add PVR functions. However, where I live right now, I get 5 TV stations over the air and none are digital yet, so there is not a lot of value in adding a tuner any time soon. My cable box and a VCR are good enough for now.

I also do not know if I want to use iTunes with my music collection or some alternative product like perhaps Song Bird.

Players and Content Retrieval

VideoLAN - VLC media player

The VLC media player at http://www.videolan.org/ is a cross-platform media player for Windows, Mac, and many flavours of Linux.

I use the Windows version on my XP machines and am very pleased with it.

The VideoLAN - VLC media player is released under the GNU General Public License.

Miro

The tag line for Miro at http://www.getmiro.com/ is "The only video player you need. Free and open-source, because open media matters.".

Miro is a free application that turns your computer into an internet TV video player with versions for Windows, Mac, and Linux.

You subscribe to video feeds (aka channels) and Miro downloads them and lets you play them.

Miro is open source.

DVD Utilities

Handbrake

HandBrake, found at http://handbrake.m0k.org/, is an open-source, GPL-licensed, multiplatform, multithreaded DVD to MPEG-4 converter, available for MacOS X, Linux and Windows.

You can use Handbrake to load your DVD's onto a hard drive for ease of access.

In my testing, it took between 30 to 60 minutes to convert a DVD using HandBrake into MPEG-4 format. I prefer to play the DVDs directly rather than storing them in my media library, but it is still a excellent and useful utility.

DVD-Assist

DVD-Assist can be found at http://mysite.verizon.net/resohjb1/Projects.html.

From the author -- "One shortcoming of Apple’s Front Row is the lack of support for VIDEO_TS folders. DVD Assist makes it possible to have a library of DVDs on your Mac and use the elegant Front Row interface to select and play them.".

Basically DVD-Assist is an Applescript "Stay Open" applet. You will need to put a tiny "Play Me" movie in each sub-directory where you have VIDEO_TS files. When you click on the "Play Me" movie in Front Row, DVD-Assist takes over and plays the movie in DVD Player instead.

Simple but extremely useful.

Display and Configuration

DisplayConfigX

DisplayConfigX at http://www.3dexpress.de/ is a program you can use to adjust your Mac to optimally match your monitor by adjusting resolutions and refresh rates outside of limited choices that your Mac has built-in.

DisplayConfigX works for free for low to mid size resolutions but purchase and registration is required for higher resolutions.

One important note -- make sure you have VNC installed and working before you use DisplayConfigX -- since if you mess up your display settings bad enough you will not be able to see your desktop on your monitor. However, VNC will allow you to work remotely and fix things back up.

That other Operating System - Windows

Since my Mac Mini sits by my TV and not in my home office, there are a bunch of utilities I use on my XP machine to assist with my media files.

7-zip

7-zip at http://www.7-zip.org/ is an open source file archiver with a high compression ratio that supports packing and unpackinh in many formats like 7z, ZIP, GZIP, BZIP2 and TAR, as well as unpacking support for formats like RAR, CAB, ISO, ARJ, LZH, CHM, Z, CPIO, RPM, DEB and NSIS.

One useful feature is that it will splice together multipart files.

7-zip is for Windows. There some "unofficial" ports for Linux and other systems like Mac but I belive most are command line versions only.

<< Older || Newer >>


 

All Tags
Business Tips
Code
Life Skills
Music
My 15 minutes
Old Articles
Photos