My Blog

This is my web coding blog. It's the home for several simple, easy-to-understand applications which are written using various programming languages. Feel free to get in touch if you want me to code a specific project for you.

PHP and web tools

Hello again! This time we will build a few simple tools that can be very useful for web developers - and not just for them.

The first one can determine is an IP address is valid or not. Here's the code.

<!DOCTYPE html>
<html>
<body>

<?php
$ip = "8.8.8.8"; // Google's Public DNS IP address

if (filter_var($ip, FILTER_VALIDATE_IP))
{
    echo("$ip is valid");
}
else
{
    echo("$ip is not valid");
}
?>

</body>
</html>

And here's what happens when we run the code above.
Ready for more? The snippet below uses just two lines of PHP code, but does a useful job, converting the first letter of each word to its uppercase equivalent.

Apparently, it seems that article titles such as "Ten Useful Web Programming Languages" attract more people in comparison with "Ten useful web programming languages". And why should we waste our precious time editing each article title manually, when the code below can take care of this tedious task automatically?

<!DOCTYPE html>
<html>
<body>

<?php
$str = "Ten useful web programming languages";
echo ucwords($str);
?>

</body>
</html>

And here's the output screen.
The last example in this article starts with a string, displays it, encrypts it, displays the encrypted string, decrypts the encrypted string, and then displays the result. Sounds like a lot of work, but the code is really simple - see for yourself.

<!DOCTYPE html>
<html>
<body>

<?php
$string = "This is the unencrypted string";
echo $string;
echo "<br>";
$encrypted = base64_encode($string);
echo $encrypted;
echo "<br>";
$decrypted = base64_decode($encrypted);
echo $decrypted;
?>

</body>
</html>

And this is what happens when we run the code.