Ever bothered thinking about how to make your site stand out a little more?
I was think about this a while back and what came to mind was sTicKy cApS,
i think that's the terminology, well anyway here is the code to create "sticky caps".
First of all we need a string to apply this function to.
So let's create a string.
<?
// This is your string, write whatever you want!
$string = 'created by derekharvey is this cool?';
?>
Now we have our string let's move onto the function that creates the sticky caps.
<?
function sticky_caps($over)
{
// php placeholder
}
?>
So now we have the placeholder for our function and the first argument of our function we can start programming.
<?
$return = "";
?>
So, first we declare the variable $return so this string can be concatenated and not display an error for some server settings.
Next, we break the string ($over) into an array or by using the function explode and using the space as the delimiter.
<?
$over = explode(' ', $over);
?>
Then we loop through each of the words and then loop through all the letters in the word and we use modulo to determine of the number is odd or even and if it's odd we capitalize the work and if not then we set it to lower case.
We assign all the words to the same array and then after each work we add a space back into the loop.
<?
foreach($over as $overs)
{
for ($i = 0; $i < strlen($overs); $i++)
{
if ($i % 2 == 0)
{
// assign the lowercase letters back into the array
$bits[] = strtolower($overs[$i]);
}
else
{
// assign the uppercase letters back into the array
$bits[] = strtoupper($overs[$i]);
}
}
// add the space beetween the words
$bits[] = ' ';
}
?>
Next we put all the array items back into the string.
<?
$overs = implode('', $bits);
?>
and then we return the value
<?
return $overs;
?>
Then to test the script we do something like this.
<?
echo sticky_caps($string);
?>
You can also put the string directly into the function.
<?
echo sticky_caps("Derek Harvey Website Developer and Programmer");
?>
This should present you with something like so:
dErEk hArVeY wEbSiTe dEvElOpEr aNd pRoGrAmMeR
--
Just to make it a little easier for you, here it the complete code:
<?
$string = 'derek harvey website developer and programmer';
function sticky_caps($over)
{
$return = "";
$over = explode(' ', $over);
foreach ($over as $overs)
{
for ($i = 0; $i < strlen($overs); $i++)
{
if ($i % 2 == 0)
{
// assign the lowercase letters back into the array
$bits[] = strtolower($overs[$i]);
}
else
{
// assign the uppercase letters back into the array
$bits[] = strtoupper($overs[$i]);
}
}
// add the space beetween the words back as we removed it when we exploded the array
$bits[] = ' ';
}
// put the peices back together
$overs = implode('', $bits);
// return the overal value
return $overs;
}
echo sticky_caps($string);
?>
Thanks for listening.
