Welcome, Guest

Please login or register

TUTORIALS SUBMENU ---->

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING

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
Featured Content

Glassy Pill Buttons
Glassy Pill Buttons
- Adobe Photoshop -
Taking Tutorial Sc...
Taking Tutorial Sc...
- Adobe Photoshop -
Wrinkle Removal
Wrinkle Removal
- Adobe Photoshop -
Inset Lines
Inset Lines
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Special Options
Printer Friendly Version
Forum Threads

Competition Discussion - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:48pm
Activity: 0 replies, 54 views
 Competition - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:46pm
Activity: 0 replies, 54 views
 PM Spamming
Author: Tamlin
Posted: Feb 06th, 1:24pm
Activity: 7 replies, 116 views
Vector Clipart Bank
Author: Crapoun
Posted: Feb 06th, 11:29am
Activity: 2 replies, 93 views
How did ...
Author: MoodsR4Cattle
Posted: Feb 05th, 6:09pm
Activity: 6 replies, 27 views
Tips and trick for Texturing/Materials
Author: noorjan
Posted: Feb 05th, 4:59am
Activity: 2 replies, 111 views
 A Billion Styles - Please Help Me!!
Author: Angelz
Posted: Feb 03rd, 6:36pm
Activity: 2 replies, 135 views
101 Things you didnt know in 3DS Max ...in fact...
Author: noorjan
Posted: Jan 31st, 6:04pm
Activity: 0 replies, 161 views
Pee Wee get's an IPad
Author: MoodsR4Cattle
Posted: Jan 30th, 4:25pm
Activity: 2 replies, 163 views
Spam :: Online hotel reservations for Hotels in...
Author: kieulinh
Posted: Jan 28th, 6:39am
Activity: 0 replies, 205 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 3 replies, 17 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 27 replies, 727 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    415