ASCII character codes

If you need to print out special characters in PHP you will probably want to get familiar with the built-in chr() function. Here’s some code that will demonstrate the various ASCII character codes available and how to output them to the screen:

<?php
print ‘Copyright (169): ‘ . chr(169);
print ‘<br />Registered Trademark (174): ‘ . chr(174);
print ‘<br />Trademark (153): ‘ . chr(153);
print ‘<br />’;

for($i=1; $i&lt;256; $i++) {
        print “$i <b>” . chr($i) . ‘</b> ‘;
}
?>

Working example here.

symfony open source php5 framework

Based on the concept of Rails / Django like framework only for PHP, here comes the MIT licensed symfony

Using file_get_contents before v4.3.0

One of the dangers of using newer PHP functions is that sometimes you’ll end up with people who are stuck on a shared host using an older version of PHP. In this case it’s helpful to have some backwards compatible functions. The category backwards compatability will deal with these situations.

file_get_contents has only been available in PHP since 4.3.0. Here is how to make it backwards compatible to version 3.xx of PHP by using file and implode:

<?php
// PHP 4.3.0 or later
$page_source = file_get_contents(“http://www.yourdomain.com/yourpage.html”);

// pre 4.3.0
$page_source = implode(, file(“http://www.yourdomain.com/yourpage.html”));
?>

Now let’s say you wanted to make your own file_get_contents function for pre 4.3.0? Here’s what you would do:

<?php
// only use this in pre 4.3.0 PHP where file_get_contents is not available
function file_get_contents($in) {
  return  implode(, file($in));
}
$page_source = file_get_contents(‘http://www.yourdomain.com/yourpage.html’);
?>

Is number odd or even?

Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an algorithms thread at Devshed. The function isn’t even necessary and could be expressed as a one-liner like this:

echo($number & 1); // $number = any integer, 0 = even, 1 = odd
 

Here’s the code to check the hours in the day starting with 0 (midnight) and ending at midnight (23):

function is_odd($number) {
   return $number & 1; // 0 = even, 1 = odd
}

foreach(range(0,23) as $number) {
        print “$number “ . is_odd($number) . ‘<br />’;
}
?>

Notes: The range function is handy for prefilling with a string or array with numbers. Now what if you want to show alternating messages, one on the even hours and one on odd hours? The following code will do just that if you change the print statement to be whatever you want to alternate:

<?php
function is_odd($number) {
  return $number & 1; // 0 = even, 1 = odd
}

if(is_odd(date(“H”))) {
        print “Since it’s an odd hour, I’ve got an odd message for you: read and be happy”;
} else {
        print “It’s an even hour, Steven.”;
}
?>

Update 10/9/08 7:16am PST: Thanks to several commenters below who pointed out that there was a missing ‘)’ — it has been added now.

Setting up a new Wordpress blog

This guide assumes you have already downloaded and unzipped/untarred the most current Wordpress version. These instructions do not cover the multi-user WP version.

Steps for setting up a new Wordpress (WP) blog:

1. Database created? Create a MySQL database for the domain.

Note: If you have more than one WP blog installed in the same database then open up wp-config.php and change the ‘wp_’ to something else like ‘wp2_’ so that you can run multiple wordpress blogs in the same database.

2. Edit wp-config.php with the database information. Create a subdirectory in the WP version locally so you can backup the wp-config locally there.
3. FTP all WP files to the target domain, including the customized wp-config.php file in step #2.
4. Run the install file from your browser. Default install location is:

domain.com/wp-admin/install.php

5. Record the admin username and password somewhere safe.
6. Delete the install files: install.php, install-helper.php
7. Create a primary user: Users -> Authors & Users -> Add New User
8. Promote this user to Level 9 (which is one level beneath the Admin level of 10).
9. Add whatever plugins you want to use. Here’s some you might try:

bad behavior - shut down the spambots
codesnippet - allows you to include code snippets inside posts
dashboard options - add PHP code snippets, RSS feeds and more to your dashboard
follow - removes the rel=nofollow links from comments
wp-cache 2.0 - cache pages to speed up loading
page navigation - adds page spanning 1,2,3… to WP pages
WP-Dash - replace the default admin area

10. Clean up the default Links area, adding/removing any default links.
11. Delete the test post and make a first test placeholder post. Something like “Hello World” but by this new user.
12. Choose a template or design your own. Starting with the default template might be helpful. There are detailed instructions at the WP codex for how to make custom WP templates.
13. Start posting!

This post will be updated over time as I’m using this for my own reference too, so might want to bookmark this one.

How to get the IP address on local Windows Machine

Step by step instructions:

1. Navigate to START - > RUN
2. Launch the command window by typing:

cmd

3. Type:

ipconfig /all

4. The IP address for the machine will show in the “Connection” section next to: IP Address.

How to add Yahoo MyWeb to Wordpress, Blogger and other weblog systems

Last night I added the Yahoo MyWeb button to the end of each post. The screenshot shows where you can find it (next to the “Listen to this post” link). Clicking this button lets users tag and save posts they find useful to their Yahoo MyWeb space. Don’t like how we categorized something? That’s fine, you can tag it however you want in your space and search it in the future through your MyWeb space.

I’ve been asked a few times how I added this to each post so I thought I’d whip up the instructions for the different blog engines we are currently using. Those instructions are below and I also add information on some other blogging clients. This post will be updated as I add more instructions for other blogging clients or links to others who have shared the instructions.

Wordpress blog system - open source
Wordpress
1. In the WP admin area, navigate to the “Presentation” tab.
2. Choose “Theme Editor”
3. Choose “Post Template”
4. Find the feedback section of the code that looks like this:

<?php comments_popup_link(__(‘Comments (0)’), __(‘Comments (1)’), __(‘Comments (%)’)); ?>

and add the following MyWeb JavaScript code directly after this:

<script language=“javascript” src=“http://sm.feeds.yahoo.com/Buttons/V1.0/yactions.js”></script>
<script language=“javascript”>
yactions.buildButton( ’save’, ‘My_Web’ );
</script>

5. Click “Update file” and the MyWeb button should now appear on every post.

Blogger blog system - host it yourself, or they will host through Blogspot for free
Blogger
1. Login to your blogger account.
2. Choose the “Template” tab.
3. Find the post-body code that looks like this:

<Item Page>

and immediately after this code, add the MyWeb button code.

4. Choose to “Save Template Changes”
5. Republish your entire blog so all posts are updated with the button.
6. See example here

Pivot blog system - open source
Pivot
1. Login to the admin area.
2. Click the “Administration” link.
3. Click the “Templates” link.
4. Choose the on the entry_standard.html template or the template you used for the main posting area.
5. Find the following code:

[[introduction]] [[body]] [[message]] [[comments]] [[commentform]]

and immediately BEFORE the [[comments]] tag, add the MyWeb button code. In my Pivot blog example I wrapped the code in p align right so the button would show up along the right side at the bottom of each post like this:

[[introduction]] [[body]] [[message]]
<p align=“right”>
<script language=“javascript” src=“http://sm.feeds.yahoo.com/Buttons/V1.0/yactions.js”></script>
<script language=“javascript”>
yactions.buildButton( ’save’, ‘My_Web’ );
</script>
</p>
[[comments]] [[commentform]]

6. Save changes.
7. Rebuild All Files by clicking on the “Maintainence” link and then choosing “Rebuild All Files”

TypePad blog system - third party pay blog hosting
TypePad
Researching/recording how this can be done. If you know how to do this using this system, please post in the comments area so this can be updated.

MovableType blog system - commercial blog program, you host
MovableType
Researching/recording how this can be done, if it can be done. If you know how to add this using this system, please post in the comments area so this can be updated.

Radio Userland blog system - commercial blog program
Radio Userland
Researching/recording how this can be done. If you know how to add this using this system, please post in the comments area so this can be updated.

No JavaScript Allowed

Some blog systems, mostly third party free hosted blogs, do not allow any JavaScript inserted into their templates. Some don’t even allow any template customization, so most likely there is no way without some special hack to add the Yahoo MyWeb, or any other type of JavaScript. The reason they do this is to avoid subjecting their users to malicious code, but unfortunately this also cripples a lot of useful code. These free third party blog systems include:

Yahoo360 blog system - third party free blog hosting
Yahoo 360
I’ve added a comment on the Yahoo blog asking them to please add this as a function. Seems like it should be a given for their own blog system, yes? If you know some other way to do this, then please use the comments/trackback and share.

Opera blog system - third party free blog hosting
Opera
Know how to do this? Please use the comments/trackback and share.

MSN Spaces blog system - third party free blog hosting
MSN Spaces
Know how to do this? Please use the comments/trackback and share.

AOL Journals blog system - third party free blog hosting
AOL Journals
Know how to do this? Please use the comments/trackback and share.

Your blog system not listed?

There are 100+ different blog systems out there at least, so to receive instructions for your blog system you have two choices:

1) provide me with a test account so I can work out how to do it, and/or
2) provide the instructions in the comments or via trackback.

I always enjoy checking out new blog systems, especially if they are written in PHP. Happy coding to you!

How to email HTML source code in one line of code

To email your HTML source code to you in one line of PHP code just use the following code:

<?php
mail(‘youremail@isp.com’,‘my source code’,file_get_contents(‘http://www.yourdomain.com/yourpage.html’),‘From webserver@localhost\r\n);
?>

MySQL 5.0 production version now available, includes subqueries

MySQL 5.0 is now ready for use on production servers touting the following features: stored procedures and SQL functions, triggers, views, cursors, information schema, XA distributed transactions, SQL mode, new federated and archive storage engines, new migration toolkit, instance manager, updated connectors, visual tools and more.

Complete list of changes in 5.0.x production.

The functionality I’m excited about is subqueries: “MySQL 5.0 supports all subquery forms and operations that the SQL standard requires, as well as a few features that are MySQL-specific.”

I’m going to install this on a machine and start checking it out.

Virtual Earth API with commercial site using PHP

Reference: Building your commercial Virtual Earth Website using PHP:

We have already seen some basic C# code behind ASP pages that proxy the search and advertising queries. However, being a web based control you are not limited to working solely with Microsoft on the server side. In this article, we will achieve the same result using PHP.

First off, the ViaVirtualEarth PHP example requires cURL [how to tell if cURL is installed]. I followed the instructions in the article to build my own example page (pictured below).

screenshot of integration with MSN Virtual Earth using PHP and cURL library

Script files required
http://virtualearth.msn.com/vems/VE_MapSearchControl.css
http://virtualearth.msn.com/vems/VE_MapSearchControl.js

Also see
Build Your Own Virtual Earth URL

« » Pages (999999) : « First ... 3 4 [5]6 7 ... Last »