The author is a web developer and a blogger. Visit his blog at Shoutbux
There are instances where you need to manipulate the string either it was inputted or taken from the database. This tutorial will help you find out what string manipulation function you can use.
What is a String?
In PHP, and every other flavor of Web programming, a string is a variable contained between quotes with a literal value. For example, $number = “2” is a string variable, whereas $number = 2 is not. The first value is seen as text only, the latter as a numeric value. Simply put, a string is a text variable.
1. ucwords - this is used to convert a phrase or sentence into a title format.
<?php
$string =”string manipulation in php”;
echo ucwords($string);
?>
2. strtoupper – this is used to capitalize every letter in a string.
<?php
$string =”string manipulation in php”;
echo strtoupper($string);
?>
2. strtolower – this is used to bring all letters in a string to lowercase.
<?php
$string =”String Manipulation in PHP”;
echo strtolower($string);
?>
3. str_replace – this function is used to replace a certain substring in a string
<?php
$string = “This tutorial is very useful for newbies”;
$newstring = str_replace(”useful”, “difficult”, $string );
echo $newstring;
?>
The str_replace() function takes 3 parameters. The first parameter is the piece of text you want to replace. The second is the text that will replace the original. The third parameter tells PHP what value to do the find and replace on.
4. strpos – find position of first occurrence of a string
Syntax: int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
<?php
$string = “Hello World”;
echo “The word World is at position number: “.strpos($string,”World”);
?>
5. substr – Return part of a string
Syntax: string substr ( string $string , int $start [, int $length ] )
<?php
$string = “Hello World”;
$substring = substr($string,6);
echo $substring;
?>
The example above will display the word “World”.
I will post more of those functions in my next tutorial.
- Related Videos
- Related Articles
- Ask / Related Q&A




Understanding HP Exam 000-433
By: Aalies Lyndon | 26/11/2009IBM is a international computer knowledge and IT consulting corporation. It is the world’s largest system integrator.
Understanding EX0-101 Certification Exam
By: Aalies Lyndon | 26/11/2009The test Institute for Information Science which is coined as EXIN is an self-governing IT exam provider.
Understanding HP Exam 000-433
By: Aalies Lyndon | 26/11/2009IBM is a international computer knowledge and IT consulting corporation. It is the world’s largest system integrator.
Understanding HP Exam 000-433
By: Aalies Lyndon | 26/11/2009IBM is a international computer knowledge and IT consulting corporation. It is the world’s largest system integrator.
Understanding HP Exam 000-433
By: Aalies Lyndon | 26/11/2009IBM is a international computer knowledge and IT consulting corporation. It is the world’s largest system integrator.
Understanding EX0-101 Certification Exam
By: Aalies Lyndon | 26/11/2009The test Institute for Information Science which is coined as EXIN is an self-governing IT exam provider.
Understanding HP Exam 000-433
By: Aalies Lyndon | 26/11/2009IBM is a international computer knowledge and IT consulting corporation. It is the world’s largest system integrator.
How to Fix Svchost exe
By: Alex | 25/11/2009Svchost.exe is an extremely vital part of Windows operating system. Svchost.exe is an important process which executes dll files and locate their libraries into windows system's memory. If there is something wrong with svchost.exe on your computer then the whole PC probably can not run stable and would be deathly slow.
Eee Top Review
By: Felix Gomez Jr | 17/11/2009 | ComputersThe Eee Top is a revolution in terms of design and price. With just less than a thousand dollar this computer is considered cheap.
First Leg of the Trip
By: Felix Gomez Jr | 17/11/2009 | TravelMactan airport. Sitting here and waiting for boarding flight 5J 564 of Cebu Pacific Air. I choose to be on this flight for 1 reason. This is the cheapest plane and the cheapest tolerable schedule which is 9:45 AM.
Dell mini Inspiron
By: Felix Gomez Jr | 14/11/2009 | HardwareDell mini is one of my favorite notebook because of its specifications and performance.
Impact of Internet connection on coffee shops
By: Felix Gomez Jr | 13/11/2009 | InternetYesterday I came to realize how internet connection as a value added service to a coffee shop have a big impact on getting customers.
Why people walk while talking on the cellphone?
By: Felix Gomez Jr | 10/11/2009 | WritingHave you ever wondered why most people when answering a call from their cellphone walks around while talking? Why is it? I have been wondering this for years.
HP Pavilion Entertainment PC
By: Felix Gomez Jr | 09/11/2009 | HardwareThis is my first time to put a review on a certain product and I think the best product to review is the one I am using. Here is the specifications of my laptop.
How to Setup Your Own Blog with WordPress
By: Felix Gomez Jr | 08/11/2009 | ProgrammingHave you tried to setup wordpress and have met some problems you were not able to solve until now? This article might help.