My first PHP from scratch
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….











gnomic says:
Too much time on your hands, eh?
OH NO! What aboot the question mark! and the Exclamation poont.
eh?