Monthly Archives: January 2008

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 . "\n";
	echo "\n\n";

Managing Office Douments with PHP

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


function write_word() {
	$word = new COM("word.application");
	$word->Visible = 0;
	$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

	//Save the document as DOC file

	//quit and release COM resources
	$word = null;


function write_excel() {
	$excel = new COM("excel.application");
	$excel->Visible = 0;

	//Create a new workbook
	$wkb = $excel->Workbooks->Add();
	$sheet = $wkb->Worksheets(1);

	//This code adds the text 'myvalue' on row 2, column 4
	$cell = $sheet->Cells(2, 4);
	$cell->value = 'myvalue';


	//close and free resources


function write_ppt() {

	$powerpnt = new COM("powerpoint.application");

	//Creating a new presentation
	$pres = $powerpnt->Presentations->Add();

	//Adds the first slide. "12" means blank slide
	$pres->Slides->Add(1, 12);

	//Adds another slide. "10" means a slide with a clipart and text
	$pres->Slides->Add(2, 10);

	//Adds a textbox
	$pres->Slides[1]->Shapes->AddTextbox(1, 20, 50, 300, 40);

	//Save the document as PPT file

	//free resources and quit powerpoint

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 their site with other hacks to see what actually made them get in the Google Top 10. A script I wrote to simplify this is below:

require_once 'httprequest.class.php';

class harvester {

	function harvester() {

	function get_site_keywords($site) {
		$req = new httprequest();
		$html = $req->get($site);
		//echo $url;
		$html = strtolower($html);
		$a = split('<meta name="keywords" content="', $html);
		$b = split('"', $a[1]);
		return $b[0];

$harvest = new harvester();
echo $harvest->get_site_keyword("");

This class uses the httprequest class.

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 ='word.application')
word.visible = true
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 =
v.Speak("this is the easiest thing to do since I have started working with ruby..")

Now this one in 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]
    name = row[1]
    address = row[2]
    city = row[3]
    state = row[4]
    zip = row[5]
    email = row[6]
    puts id+','+name+','+zip

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 =

This way I was able to invoke a web camera testing program written in java with a few environment variable calls

ENV['CLASSPATH'] = '../../JavaRobots/dist/javarobots.jar;../../JavaRobotsLib/jai_core.jar;../../JavaRobotsLib/jai_codec.jar'require 'rjb'test = Rjb::import('')
c ="c:\\temp\\fcover.jpg")
#default_close_operation = JFRAME::EXIT_ON_CLOSE
sleep 5

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 ='InternetExplorer.Application')
ie.visible = true