PHP String

In this tutorial today we will play with strings and php string functions. So before starting the topic first understand what is string in php.

Definition : String is a sequence of any type of characters, for example (” Hello phpkeeda “). This  is a sequence of characters.

How strings are declared in php

In php, Strings are declared inside double quote (” “)  or  within the single quote (‘ ‘). for example.

The above are all about the declaration of php string. Now we will learn some important string functions in this lesson.

Functions of php strings

These are some functions of strings in php about which we will study in this tutorial.Since these functions are php native functions,  provided itself by php technology, hence we do not declare any function for this.

Remove whitespaces and other character

trim() => trim is used to remove whitespace or other character from both end of the string.


Replace value with some other value

str_replace() => str_replace() is used to replace some value with other value.

  • str_replace() takes three parameter inside itself,
  • First parameter is value which we want to find
  • Second parameter is those value which will replace the first paramter value
  • Third parameter the the content in which we have to perform this operation,Example  :   str_replace(“find”, “replace”, “content”);

Break string into array

explode() => explode() is used to split string into an array. We have not discussed the topic array so as of now just understand how it works, In the array tutorial i will explain about this. let’s see an example how it works.

It takes two parameter as demonstrated :   explode ('@',  '') ;

  • First parameter takes the character on the basis of it will split the string (here @ is first parameter).
  • Second parameter is the string itself which we want to break into array (here the string used is The first and second parameter must be seperated by commaa(,) and the first parameter can be anything but not empty even a single space can be used.


Obtain string from the array element

implode => implode() is used to return a string from the array of the element.

We will also see this tutorial again while learning array in coming tutorials.

Implode and explode are vice versa of each other, explode breaks an string to array, while implode creates a string from the array elements.

It also takes two parameter :   implode(" ", $arrays)

  • First parameter is the value on which we have to combine the array elements (here ” “(space) is first parameter ).
  • Second parameter is the array on which we have to perform the implode.


Count length of the string

strlen() => strlen() is used to get the number of characters in a string.

You can use this function in form validation when you have to get 10 digit mobile number when user submit the form.


Reverse a string in php

strrev() => strrev() function is used to display the string in reverse format.

It means (HELLO => converted into  OLLEH after using this function).


Count number of words in a string

str_word_count () => str_word_count() is used to get the number of words in a string.


Find a particular text inside a string

strpos() => strpos() This function returns the position of a text inside the string.

strpos() function takes two parameter inside it. you can say strpos is short form of string position,

  • First parameter is string itself inside which the @ symbol is present
  • Second parameter is the text which you want to find like @ symbol.

like :  strpos(“”, “@”);

Suppose we want to get the position of @ symbol inside a string. We will write


Convert String into lower case

strtolower() => strtolower() is used to converts the string into lowercase

Convert string into upper case

strtoupper() => strtoupper() is used to convert string into uppercase.


Convert first letter of string to upper case

ucfirst() => ucfirst() is used to convert string’s first letter to uppercase.

* In order to convert first letter of string to uppercase the given string’s first letter must be in lowercase.


Convert first letter of string to lower case

lcfirst() => lcfirst() is used to convert string’s first letter to uppercase.


Convert each first letter of string to uppercase

ucwords() => ucwords() is used to convert each first letter of given string to uppercase.


Remove particular text from the left of the string

ltrim() => ltrim() function is used to eliminate some string from the left side of the string.


Remove particular text from the right of the string

rtrim() => rtrim() function is used to eliminate some string from the right side of the string.

It works similar as ltrim() the difference is quite simple that it removes characters from the right.


Leave a Reply

Your email address will not be published. Required fields are marked *