Archive for January, 2008

Started New Tech Blog CodeGin.Com

I have started a new tech blog called codegin.com. This blog is a place where I can post tips, hacks, recipes, and code examples about everything.
I am really excited about this new blog, please visit to check it out.

Creating a Web Spider with PHP

I used this for spidering my site.
function get_links($url) {
$req = new httprequest();
$html = $req->get($url);
$regex = ‘/mhref/m’;
$preg = ‘/<a href=”([^0-9].+?)”/’; // 1 or more
$preg = ‘/<a href=”([^0-9].*?)”/’; // 0 or more
$urls = array ();
preg_match_all($preg, $html, $urls);
$count = 0;
foreach ($urls as $url => $links) {
if ($count == 1) {
foreach ($links as $link) {
echo “link-> ” . $link [...]

Managing Office Douments with PHP

I decided to write a class to manage office documents with PHP.
Word:

function write_word() {
$word = new COM(“word.application”);
$word->Visible = 0;
$word->Documents->Add();
$word->Selection->PageSetup->LeftMargin = ‘2″‘;
$word->Selection->PageSetup->RightMargin = ‘2″‘;

//Setup the font
$word->Selection->Font->Name = ‘Verdana’;
$word->Selection->Font->Size = 8;

//Write some text
$word->Selection->TypeText(“This is a test document”);
//Insert an image
$word->Selection->InlineShapes->addPicture(“C:\\temp\\test.png”);

//Save the document [...]

Get Competition Keywords

One thing I found useful when searching on keywords for SEO was to Google the keywords I wanted to “own”, then grab all the top 10 results from Google. Then I would go to the site and “View Source” to see what keywords they were using and then would manually go back and review [...]

Getting Text From The Internet with PHP

This is a class very close to the Ruby version to get text.

<?php
class httprequest {

function httprequest() {
}

function get($url) {
if (substr($url,0,7) != “http://”){
$url = ‘http://’.$url;
}
$html = implode(”, file($url));
return $html;
}

function post() {

}

}
?>

Adding Text To Microsoft Word with Ruby

I actually created this to automate the cut and paste of text from the web to a word document for consumption by my “plain-text-challenged” customers.

require ‘win32ole’
word = WIN32OLE.new(‘word.application’)
word.visible = true
word.documents.add
word.selection.typetext(“Hello World!\n”)

Text To Speech (TTS) with Ruby and PHP

I was working with some scripts for having my PC talk. The first one is in Ruby:

require “win32/sapi5″
include Win32
v = SpVoice.new
v.Speak(“this is the easiest thing to do since I have started working with ruby..”)
Now this one in PHP:
<?php
$tts = new COM(“SAPI.SpVoice”);
$tts->speak(“this is a test”);
?>

Getting Data From MySQL Using Ruby.

I wanted a fast way to get data from mysql using ruby. after i installed the mysql gem it was really easy. This program gets all rows, but only displays the id,name and zip.

require “mysql”

dbh = Mysql.real_connect(“localhost”, “root”, “password”, “scottwork”)
res = dbh.query(“SELECT name,address,city,state,zip,email FROM contacts”)

while row = res.fetch_row do
id = row[0]
[...]

Invoking Java From Ruby

I did this using the rubygem Java-Ruby-Bridge. I did this because I was having problems with JRuby and wanted to execute my class from within Ruby.
Here is a simple way to do it, as long as you remember the classpath.
require ‘rjb’
Rjb::load(classpath = ‘../../TestJava/bin’, jvmargs=[])
test = Rjb::import(‘Test’)
c = test.new
c.foo3
This way I was able to invoke a [...]

Opening Internet Explorer with Ruby

I have been doing a lot of web automation these days for testing and automating web based applications without APIs.
To open Internet Explorer with Ruby just use OLE.
require ‘win32ole’
ie = WIN32OLE.new(‘InternetExplorer.Application’)
ie.visible = true
ie.gohome