Recent entries tagged "html"

My first PHP from scratch

Posted 07/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….


Tags: , , ,
Back to top

Annoying Web design

Posted 07/24/08

I don’t understand why people think certain features make their Web pages better or more appealing, when all they really do is annoy the %$#^&* out of visitors.

I know I’m going to revisit this topic as soon as I come across another of those things that drive me crazy, but here are three.

1. Highlighting every search term you used to find the page. For example, if I Google “how do I change my oil,” there are some sites that will highlight every instance of each of those words, often in different colors. That makes it incredibly annoying to read. I get the idea, but there’s gotta be an off switch. (And I have no idea what Matt Roggott and Brian Suda were drinking when they wrote “Enhance Usability by Highlighting Search Terms” for A List Apart.)

2. Too many darned links. It makes sense to link to major terms, and certainly to Web pages that you reference, but some pages — I notice this frequently with news sites — link dozens of words even if they have nothing to with the story. Usually these are company names that link to the company home pages (not very useful) or stock price (ditto), but sometimes it’s worse. Some will links any term that corresponds to a section on the site. So every occurrence of the word “software” will link to the software category.

Wikipedia can get nuts like this too, with links to every conceivable page. “John Doe was the manager of a grocery store on a side street in downtown Atlanta during the opening weeks of World War II.” It’s one thing to link to significant entries, but to words that just have a definition (a la “week”)? Come on.

3. Gray body text. Why? Why? Are you trying to save ink by not using black? Does gray make the page more readable? No, in fact the opposite. Lower contrast just makes it harder to see. Is it a style thing? If so, it’s a stupid one.

4. Rollover ads. You know these things — highlighted text that pops up a small ad if you dare to accidentally roll your mouse over one. Companies pay to be linked to certain terms; maybe you would roll over the word “audio” and — bam! — up comes an ad for iTunes or whatever. Incredibly, incredibly annoying. Luckily there are Firefox extensions to shut that off, but still. Come on, guys.

 

More to come, I’m certain.


Tags: , ,
Back to top



Site created with

and


Blog run by