Useless Web Projects: Chapter 1 This is really useless
Posted by Dave Ferrite on 10 Apr 2008 at 11:09 am | Tagged as: Uncategorized
Ok, it’s about time I started some posts that weren’t just PocketFlock updates or extension reviews.
So here’s my first new segment in a long time, Useless Web projects.
This weeks project involves playing around with some different PHP and javascript functions that normally are useful.
I’m assuming you’re already have a passing knowledge with HTML, Javascript, or PHP, though if you’re not, keep reading as I’ll gradually explain everything.
Let’s just go over the internal functions and variables we’ll be using first and what they do
I'm broke
PHP
explode(separator, source-string)
explode is one of those really useful functions for dealing with very long strings. It allows you to take a very long string (our source-string, if you will) and break it apart into an array using a separator string.
i.e. $ourarray=explode(” “, “This is a fantastic function”) would break the string by spaces into an array $ourarray[] where $ourarray[0]=”This”, $ourarray[1]=”is”, $ourarray[2]=”a”, etc…
sizeof(ourarray)
sizeof is another useful array function, basically doing exactly what it sounds like. Your result is the size of the array in question. Using our previous array as an example, sizeof($ourarray) would equal 5.
str_replace(replacethis, withthis, inthis)
str_replace replaces a given substring (replacethis) with another substring (withthis) in a larger string (inthis).
i.e. str_replace(”Flock”, “Firefox”, “Firefox is my favorite Gecko browser!”) would output “Flock is my favorite Gecko browser!”
ps, like most functions that return a result, str_replace can be nested or even used as arguments.
What do you think str_replace(”What should I do?”, “Where am I?”, str_replace(”I’m drunk”, “I’m lost”, “I’m lost. Where am I?”))
implode(joint, ourarray)
implode is the opposite of explode. With this function you can collapse an array (ourarray) into a string using another string (joint) to join it.
Using our earlier array, and applying this implode(”-”, $ourarray) would give you “This-is-a-fantastic-function”
We could use all these functions to perform something useful on a complicated string.
If you were to create a new php file on your server with the following lines:
$firststring="*I've added some sparkly stars*They could get really annoying*but I'll use them as linebreaks";
echo $firststring
$firststring=str_replace("but I'll", "if I didn't");
$ournewarray=explode("*", $firststring);
echo implode("\n", $ournewarray);
The “\n” inserts a newline character on most *nix systems and would give you a plain text document with the following output.
*I’ve added some sparkly stars*They could get really annoying*but I’ll use them as linebreaks
I’ve added some sparkly stars
They could get really annoying
if I didn’t use them as linebreaks
-END OF USEFUL INFO-
Now let’s make that useful stuff infinitely more useless and complicated.
Chapter 1 Useless Project
Tags: useless, php, javascript, html, css