spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Introduction to PHP Programming

By PHP Catalyst
2007-11-19


Manipulating Strings in PHP

String Manipulation in PHP

In this section, we will learn different ways of manipulating string through PHP coding. Some of these are basic coding techniques while some will require using built-in functions of PHP for advance string manipulation.

String Concatenation

One of the most simplest string manipulation activity that can be done is concatenating two or more strings to form a single string. For example: $a might hold a string called "First" and $b might hold a string called "Second". To concatenate these two strings into one, we use a '.' (dot). See this example:

<?php
$a 
"First";
$b "Second";
$c "Third";

echo 
$a $b//using a . (dot) in between $a and $b

echo $a $b $c//three strings together

$d $a $b//concatenating $a, $b and storing in $d

echo $d//printing $d

echo "\$a is holding a string called"." ".$a;  
?>

Change the case of a string to lowercase and UPPERCASE

That's quite easy! All we need to do is use the built-in function called strtolower() to convert a string to lowercase and strtoupper() for uppercase conversions. See the example to convert a string to lowercase and uppercase.

<?php
$string 
"PHP is a GREAT Programming Language!";
$new_string strtolower($string);

echo 
$new_string;
//Output: php is a great programming language!

$new_string strtoupper($string);

echo 
$new_string;
//Output: PHP IS A GREAT PROGRAMMING LANGUAGE!
?>

Now, use the above example but replace the used functions with ucwords() (Uppercase the first character of each word in the string) and also ucfirst().

Determining position of first occurrence of a string

This is easy one! We will use yet another handy built-in function called strpos(). The syntax of strpos() is:

strpos ( string, keyword [, int offset] )

This will return 'FALSE' if the keyword is not found in the string and you will have to use '===' (strict comparison) to check the returned value. See the below example for usage of strpos().

<?php
$string 
"PHP is a GREAT Programming Language!";
$keyword "GREAT";

#Searching for keyword in the string
$position strpos($string$keyword);

echo 
$position//Output: 9 (9th position of Keyword)
?>

<?php
#conditional usage of strpos()
$string "This is a simple string";
$keyword "simple";
$keyword_1 "MySQL";

if (
strpos($string$keyword) > ) {
    echo 
"String found";
    } else {
    echo 
"String not found";
    }
?>

<?php
#conditional usage of strpos()
$string "This is a simple string";
$keyword "MySQL";

if (
strpos($string$keyword) > ) {
    echo 
"String found";
    } else {
    echo 
"String not found";
    }
?>

Return portion of a string from a given string

To get portion of a string from a given string we will make use of substr() function of PHP. The syntax is:

substr ( string , int start [, int length] )

Remember, the position of the first character of the string is always '0'. The function will take the start position and return a portion of the string from the start position as specified. You may optionally also mention the end position till which you want to get the portion of any given string. For example, in the string "Hello", the letter 'H' is at position '0' and the letter 'o' is at position '4'. See this example:

<?php
$string 
"PHP is GREAT Programming Language!";

#Returning portion of the above string from 0 to 12
$str substr($string,0,12);

echo 
$str//Output: PHP is GREAT (includes two spaces as well)

#Let's also concatenate something to $str

echo $str." Programming Language!";
?>

Adding slashes to string

Under various circumstances, you will need to automatically add slashes to escape special characters in a string. Be it storing in database table, or for anything else. Here's how we can do it with built-in addslashes() function. The syntax is:

addslashes ( string )

Below is an example to add slashes to any given string.

<?php
$string 
"Let's learn PHP. It's fun!";
$new_string addslashes($string);
echo 
$new_string//output: Let\'s learn PHP. It\'s fun!

#You can also do this directly without storing it in $new_string
echo addslashes($string); //works too! 
?>
That's all folks! We will later on add more examples here. You may visit the examples section for other examples. With this section, we have completed our session on Strings in PHP.

Tutorial Pages:
» What can I do with PHP?
» Popoular Features of PHP
» Basics of PHP
» Variables in PHP
» Data Types in PHP
» Expressions and Operators
» Control Structures in PHP
» Functions in PHP
» Declaring Functions in PHP
» Scope of a Variable
» Built-in Functions in PHP
» Handling Strings in PHP
» Printing Strings in PHP
» String Comparisons in PHP
» Manipulating Strings in PHP
» Arrays in PHP
» Types of Arrays in PHP
» Creating Arrays in PHP
» Array Operations in PHP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: