My first PHP from scratch

Published 7/25/08

PHP is a powerful scripting language, and incredibly popular on the Web (WordPress, which powers this and millions of other blogs, it itself written in PHP.) I had edited plenty of it, but now I finally wrote my first code from scratch.

It’s about as insignificant as you can get, aside from “Hello World,” but I’m pleased.

But making it made me realize why I can’t be a programmer. I’m way to fixated on the details.

This started because I found a joke site, Adrian Speyer’s “Convert American English to Canadian English” because I’m working on a story about working with immigrants, and made a joke in it about speaking Canadian.

Anyway, the site I found doesn’t do much more than add “eh?” to the end of whatever’s typed in, and turn some “er” words (theater) into “re” words (theatre). It’s a cute joke, but the execution is poor. Enter “This is a test” and you get “This is a test eh?” That’s OK, but it’s missing a comma. But enter “This is a test.” (with a period) and you get “This is a test. eh?” Messy.

And so I had to write my own, just because.

It had to do the same thing — as well as a few more things — but it had to work with large blocks of text, too. And with periods.

So I had to work to make mine — it’s at www.kantor.com/translate — better. For one, it adds “, eh” to the end of every sentence. But it also checks to see if the block of text ends in a period. If so, it inserts “, eh” before the period; if not, it adds it to the end.

The site I found also has another flaw. If you enter “This is a test.” and then hit Enter, you’ll end up with a blank space below, so the translator returns

This is a test.
eh?

No good. So mine has to strip any blanks, tabs, or returns from the end.

All of this is pretty simple (unless, like me, you know nothing of PHP), but I found myself getting caught up in all the details. What if someone put in text that already had an “eh” at the end? I didn’t want “eh eh,” so it had to filter it. And so on. Too many nitpicks, I think. If I was doing this for a living I’d drive myself bats.

In case you care, here’s the final code; it pulls the variable $text from the form on the page:

 

<?php

$text = $_POST['text'];

$text = trim($text); //Removes any white space

//Replace every period with “, eh.”;
    $text = str_replace(”.”, “, eh.”, $text);

// Add “, eh” at the end of the whole string, unless it already ends with eh.
// Get the last three characters
    $lastchar1 = $text[strlen($text) -1];
    $lastchar2 = $text[strlen($text) -2];
    $lastchar3 = $text[strlen($text) -3];
    $ender_eh=0;

//Does it end in “eh” or “eh.”?
    if ($lastchar2 == “e” AND $lastchar1 == “h”)
        {$ender_eh=1; $period=0;} //Already ends with eh

    if ($lastchar3 == “e” AND $lastchar2 == “h” AND $lastchar1 == “.”)
        {$ender_eh=1; $period=1;} //Already ends with eh and a period
    if  ($ender_eh == 0 AND $period == 0)
        $text = $text . “, eh” ; // add eh

    if ($ender_eh == 0 AND $period == 1)
        $text = $text . “, eh.” ; // add eh;

$text = str_replace(”about”, “aboot”, $text);
$text = str_replace(’aluminum’, ‘aluminium’, $text);
$text = str_replace(”center”, “centre”, $text);
$text = str_replace(”theater”, “theatre”, $text) ;
$text = str_replace(”color”, “colour”, $text) ;
$text = str_replace(”flavor”, “flavour”, $text);
$text = str_replace(’humor’, ‘humour’, $text);
$text = str_replace(’cheese fries’, ‘poutine’, $text);
$text = str_replace(’bathroom’, ‘washroom’, $text);

//And many more…

echo $text;

?>

 

I grabbed a lot of word examples from Speyer’s code (’jewelry’ and ‘jewellery,’ for example.) My thanks to him.

Next I want to add a random generator so it doesn’t add “eh” to every sentence — just some….

Add to del.icio.us Digg it! Add to Technorati Add to Furl Add to reddit Stumble it!

The Fray


gnomic says:

Too much time on your hands, eh?

OH NO! What aboot the question mark! and the Exclamation poont.

eh?

July 25th, 2008 at 6:46 PM

Steven Rumbalski says:

just for fun, here it is in python:
>>> def EngToCan(text):
text = text.strip()

replacements = [('. ',', eh. '),
('about','aboot'),
('aluminum','aluminium'),
('center','centre'),
('theater','theatre'),
('color','colour'),
('flavor','flavour'),
('humor','humour'),
('cheese fries','poutine'),
('bathroom','washroom') ]
for old, new in replacements:
text = text.replace(old, new)
if not text.endswith(’eh’) and not text.endswith(’eh.’):
if text[-1] == ‘.’:
text = text[:-1] + ‘, eh.’
else:
text += ‘, eh’
return text

>>> print EngToCan(”There was a square of aluminum foil in my cheese fries. I find no humour in this. It ruined their flavor. I’ll have to complain at http://www.outback.com.”)
There was a square of aluminium foil in my poutine, eh. I find no humour in this, eh. It ruined their flavour, eh. I’ll have to complain at http://www.outback.com, eh.

July 26th, 2008 at 5:26 PM

Steven Rumbalski says:

oops. the leading spaces were trimmed from my comment. unfortunately indentation is important in Python. Here’s the updated code (pretend the underscores are spaces):

def EngToCan(text):
____text = text.strip()
____
____replacements = [('. ',', eh. '),
____________________('about','aboot'),
____________________('aluminum','aluminium'),
____________________('center','centre'),
____________________('theater','theatre'),
____________________('color','colour'),
____________________('flavor','flavour'),
____________________('humor','humour'),
____________________('cheese fries','poutine'),
____________________('bathroom','washroom') ]
____for old, new in replacements:
________text = text.replace(old, new)
____if not text.endswith(’eh’) and not text.endswith(’eh.’):
________if text[-1] == ‘.’:
____________text = text[:-1] + ‘, eh.’
________else:
____________text += ‘, eh’
____return text

July 26th, 2008 at 5:39 PM

Admiral says:

I could never code… Tedious.. Death would be a sweet release from that hell. lol

July 28th, 2008 at 4:49 PM

Weigh in

Yer name:

Yer e-mail (to be notified of responses or I can respond privately -- never ever shared):

Yer Web site (if you like):

What you have to say (Be civil, or it might be removed; comments with links
might be held for moderation, just so you know):




Site created with

and


Blog run by