 |

Welcome to yet another scrowler tutorial on PHP. This
tutorial covers various different text manipulation and management functions,
many of which are absolutely invaluable for your scripts.
Topics that we will cover in this tutorial include:
- Text length limiting
-
HTML formatting
-
Replacing letters or words
-
Form posted text
- Hashing strings (one way encryption, inc. mhash lib)
-
Number manipulation
-
Padding strings
-
Converting text cases
-
Word wrapping
Woah! What a list! Let’s get started then! This tutorial is written for the
average PHP beginner to get a firmer grasp on the world of string management
with PHP, including some basic math functions. The content of this tutorial is
optimized to include web functionality i.e. stripping HTML tags from a string,
converting HTML entities, etc.
You will even be taught how to hash strings, going far into the mhash
PHP extension library.
First up on the agenda: Text length limiting.
Text length
limiting
Text length limiting is a very useful feature of PHP, and it’s relatively easy
to achieve. Using inbuilt functions of PHP you can cut a string off at a certain
point, and you can even check its length with a single function call!
So, let’s say that we have a submission form for a local soccer club to allow
coaches to submit overviews of weekly soccer matches. The administrator doesn’t
want his database to get huge, because he knows how many teams there are in the
club, and he suspects that coaches might get carried away! So what does he do?
The answer is simple - He imposes a text length restriction!
On Monday, the administrator is feeling rather harsh, so he decides to use
substr() to simply cut everything off the string from a certain point onwards,
to limit the length. He reads this tutorial and finds out the syntax for substr()
is as follows: [string], [start], [length].
He checks the database and it’s huuuuuge! How can he manage to allow so much
data to go in every week? Fortunately, he understands the concepts in this
tutorial and is able to solve his problem in a harsh way. Here is the code
he decides to use:
<?php
function cutText($string)
{
$string = substr($string, 0, 5000);
return $string;
}
$string = cutText($_POST['overview']);
?>
|
This works successfully! If a coach inputs an overview
larger than 5000 characters, anything past the 5000th character is
cut off!
So the week goes by, the next soccer round commences, and a coach goes to
submit his weekly overview. He loses half of it, and what does he do?
He complains of course! How unfair - They didn’t even back it up!
The administrator decides to be a little nicer this time, and instead of
just cutting the end off, he decides to tell the user if it’s too long, so
they can cut it down to size manually. For this, he uses the function strlen() which
simply takes the input argument [string] and returns its length.
<?php
function checkLength($string)
{
$length = strlen($string);
if($length > 5000)
{
return false;
} else {
return true;
}
}
$text = $_POST['overview'];
if(checkLength($text) == true){
# process the data
} else {
echo 'Sorry for the trouble, your data was longer than 5000
characters, could you please shorten it and retry? Thanks!';
}
?>
|
Problem solved! The soccer coach now has the opportunity to shorten
his
overview instead of completely losing the excess! This gives us a happy administrator, as his database remains a manageable size, and
happy coaches as they can submit their overviews with ease!
That concludes the section on text limiting, so let’s move on to HTML
formatting!
- Tutorial written by Scrowler
|  |