Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Text & Number Management Functions

pages (4): [1] 2 3 4


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

Pages (4): [1] 2 3 4 Next>
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Planetary Masses
Planetary Masses
- Adobe Photoshop -
Retro Style Text
Retro Style Text
- Adobe Photoshop -
Xpresso Tutorial -...
Xpresso Tutorial -...
- Maxon Cinema 4D -
Rock Textures
Rock Textures
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1051 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2355 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2498 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673