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 in PHP

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 examples 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) > 0 ) {
    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) > 0 ) {
    echo "String found";
    } else {
    echo "String not found";
    }
?>

Return substring of a string using PHP

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 tutorials on Strings in PHP. If you want to see more detailed information on this section, please contact us and let us know.

Further reading on strings in PHP: