No Problem!
Just let us know what you want and we’ll get back to you with a price. Simple.
Please fill in all fields marked * Your Message*

Your Name*
Your Email*
t: 0113 244 9399 // e: [email protected]
website designers based in Leeds

the web designer’s Notebook

Web Designer's Notebook

Welcome to the web designer’s notebook! We try to feed web designer’s with a relevant source of information relating to web design and development. Enjoy!

Design Trends April 26th, 2010
Add to add Add this design post to Delicious Add this design post to Stumbleupon Add this design post to Digg

PHP Basics: Playing With Strings



PHP Basics Strings This tutorial is for PHP beginners wanting to learn some basic functions in PHP. Its focus is on functions used to manipulate and display strings. The tutorial explains how to extract substrings from longer strings, replace substrings within a string and explode a string into an array, as well as other things. The structure of the tutorial explains each function, shows a working example and then suggests practical ways in which to use them.


Sub strings

substr()

substr() is used to remove or grab sections of a string. If your string is “Use the force Luke” and you just want to grab the word “force” then substr() is what you need.

substr(string, begin, end);
// string is your original string, in this case 'Use the force Luke'.
// begin is the starting character index.
// end is the number of characters to grab.
// The character at the start of the original string is index 0, so the f of force would be character index 8.

<?php
echo substr('Use the force Luke', 8, 5);
// Prints out - force
echo substr('Use the force Luke', 4);
// Prints out - the force Luke
echo substr('Use the force Luke', -4, 4);
// Prints out - Luke
?>

substr_replace()

substr_replace is very similar to substr(), but you don't just remove a section of string you replace it with something else.

substr(string, replacement, begin, end);

<?php
echo substr_replace('Use the force Luke', 'lightsaber', 8, 5);
// Prints out - Use the lightsaber Luke
?>

strlen()

strlen() tells you how many characters are in a string.

strlen(str);

<?php
echo strlen('Use the force Luke');
// Prints out - 18
?>


Practical Examples

Character Limit of 20 on a string:
$string = 'I have got a bad feeling about this.';
$string = substr($string, 0, 20);
echo $string;
// Prints out - I have got a bad fee

Remove Last 3 characters of a string:
$string = 'I have got a bad feeling about this.';
$string = substr($string, 0, strlen($string) - 3);
echo $string;
// Prints out - I have got a bad feeling about th

Always making a word plural (checking to see whether the last letter of a string is an 's' and if not adding one).
$string = 'parrot';
$last_letter = substr($string, -1);
if ($last_letter != 's') {
      $string = $string . 's';
}
echo $string;
// Prints out - parrots

note - != means 'does not equal' and a dot adds two strings together, so “hell”. “o” would be “hello”.


String Replacing

str_replace()

str_replace() is similar to substr_replace. With str_replace() you specify the actual string you want to remove, not the character indexes.

str_replace(substring, replacement, string, n);
// n is the number of times to replace

<?php
$string = 'Grido shot first';
echo str_replace('Grido', 'Han', $string);
// Prints out – Han shot first
?>


Practical Examples

Getting rid of all occurrences of a bad word in a string.
$string = 'These are swear words - balls blast and bugger';
$bad_word = 'balls ';
$string = str_replace($bad_word, '', $string );
echo $string;
//Prints out - These are swear words - blast and bugger

Getting rid of the first 3 occurrences of a bad word in a string.
$count = 3;
$string = str_replace($bad_word, '', $string, $count );

Deleting swear words in a string.
$swear_words = array('bugger', 'balls', 'blast');
$string = str_replace($swear_words, '*[email protected]%', $string);

// Prints out - These are swear words - *[email protected]% *[email protected]% and *[email protected]%

note – str_replace() is a great little function but it is only practical for small sentences. For larger paragraphs you may need to look into regular expressions.


Uppercase or Lowercase

strtoupper();

strtoupper() makes all characters in a string uppercase.

strtoupper(string);

<?php
echo strtoupper('UsE thE foRce LUKe');
// Prints out – USE THE FORCE LUKE
?>

strtolower();

strtolower() makes all characters in a string lowercase.

strtolower(string);

<?php
echo strtolower('UsE thE foRce LUKe');
// Prints out – use the force luke
?>

ucwords();

ucwords() makes the first letter of each word in a string uppercase.

ucwords(string);

<?php
echo ucwords('use the force luke');
// Prints out – Use The Force Luke
?>


Practical Examples

Converting a string into the title of a page. What we need to do here make a function that converts the whole string to lowercase, then capitalizes each word but not words that shouldn't be capitalized like 'and', 'of', 'the', 'or'. Lastly we need to make sure the first letter of the string is definitely a capital.

Function make_title($string) {
     
// make string lowercase
      $string = strtolower($string);
     
// capitalize all words
      $string = ucwords($string);
     
// replace certain words back to lowercase
      $lowercase_words = array('and ', 'the ', 'of ', 'an ', 'or ', 'a ');
      $uppercase_words = array('And ', 'The ', 'Of ', 'An ', 'Or ', 'A ');
      $string = str_replace($uppercase_words, $lowercase_words, $string);
     
// make sure first letter of string is a capital
      return ucfirst($string);
}

echo make_title('This is the title of my page');
// Prints out – This Is the Title of My Page

note – ucfirst() makes the first letter of a string capital.


Implode and Explode

explode()

explode() splits up a string into pieces and makes an array with those pieces.

explode(seperator, string);
// seperator is the character or string that will trigger a split, usually a comma or full stop.

<?php
$string = 'banana, apple, orange, strawberry, plum';
$array = explode(',', $string);
echo $array[0];
// Prints out - banana
?>

Now, $array is an array with five values :
Array
(
      [0] => banana
      [1] => apple
      [2] => orange
      [3] => stawberry
      [4] => plum
)

implode()

implode() does the opposite of explode(). If we use the array we made with explode() above we can convert it back to a string.

implode(seperator, array);

<?php
$string = implode(',', $array);
echo $string;
// Prints out - banana, apple, orange, strawberry, plum
?>



Other Useful String Functions

trim()

trim() trims white space from a string.

<?php
$string = '     white space either side of this         ';
echo trim($string);
// Prints out – white space either side of this
?>

strip_tags()

strip_tags() strips a string of any html tags or php tags.
strip_tags(string, allowable tags);

<?php
$string = '<p>I am a p tag, </p><!-- I am a comment --><a href=”#”>I am an anchor tag, </a>';
echo strip_tags($string);
// Prints out – I am a p tag, I am an anchor tag,
echo strip_tags($string, '<p><a>');
// Prints out – <p>I am a p tag, </p><a href=”#”>I am an anchor tag, </a>
?>

Search


Search Web Designer Notebook Search Web Designer Notebook



Catergories
Web Design Leeds - Catergories


Twitter Rabbit Hole Designs Leeds Twitter
follow us...
"Promoted tweets went live today, looks like Twitter are finally trying to make some money, K-chiiiing! - http://bit.ly/9VCCbX
rhdesignsltd
" We now feature on CSS Heaven! - http://bit.ly/arUy8V it's wicked to see our site being showcased for web design inspiration. Cheers guys!"
rhdesignsltd
Our RSS feed for The Web Designer’s Notebook is now working again, thanks @crossinghippos for the heads up - http://bit.ly/aY1iIm"
rhdesignsltd


Ads

Cheap Hosting
Web Tutorial (t)
Write your comments here...

*Name: Website:
*Email:
*Message:

previous postsBack to posts
current projects

• LLimah International Ltd
• Chapel Allerton Tennis, Squash & Gym
• Milfield Primary School
• Freddy Fit
• Warren v10.2
• Raquette Bistro
UPCOMING EVENTS

High Tech Talent - 08.04.10
Future Web Design London - 17.05.10
New Designer’s - 01.06.10
Latest Thoughts

PHP Basics: Playing With Strings - 2010-04-26
Play Super Balloon Popping Frisbee Thrower 3000! - 2010-04-23
Seen Google Maps Street View in 3D yet? - 2010-04-07
Easy guide to CSS3 @fontface - 2010-04-01
SEO Basics - Where To Place Keywords In Your Webpage - 2010-03-26
Inspiring Web Typography - 2010-03-26
Notebook Official Launch this Wednesday! - 2010-03-23
See more...
Why not Drop By?

Rabbit Hole Designs
25 Shafton Lane
Leeds, West Yorkshire
LS11 9RE
Call. 0113 244 9399
Email. [email protected]

Rabbit Hole Designs Studio | Website designers Leeds