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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// string declaration $name = " You are learning php from phpkeeda "; <strong>Or</strong> $name = ' You are learning php from phpkeeda ' ; // even you can write number inside single or double quote. // Which will be treated as string // try this $x = '123'; var_dump($x); Output will be 123 and its data type will be string . |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php echo 'Before triming lenth of string is : '; echo strlen(' hello phpkeeda '); // Output : 20 echo '<br><br>'; echo 'After triming length of string is :'; echo strlen(trim(' hello phpkeeda ')); // Output : 14 // Here first trim function runs and then strlen, // strlen returns the length of string after trim ?> |
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”);
1 2 3 4 |
<?php echo str_replace("phpkeeda", "User", "Hello phpkeeda"); // output : Hello User ?> |
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 ('@', 'rahul78275@gmail.com') ;
- 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 rahul78275@gmail.com). 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.
1 2 3 4 5 6 7 |
<?php $str = "rahul, shivam, jack, joe, pooja"; var_dump(explode(",", $str)); // breaking on comma(,) //array(5) { [0]=> string(5) "rahul" [1]=> string(7) " shivam" [2]=> string(5) " jack" [3]=> string(4) " joe" [4]=> string(6) " pooja" } ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $arr = array('one','two','three','four!'); echo implode(" ",$arr); echo "<br><br>"; // Output : one two three four! echo "second example <br>"; echo implode("-",$arr); // here we use hyphen (-) as joining these array element // Output : one-two-three-four! ?> |
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.
1 2 3 4 5 |
<?php echo strlen("Hello phpkeea!"); //Output : 14, It also count space as a character length. ?> |
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).
1 2 3 4 5 |
<?php echo strrev("Hello phpkeeda"); // Output : adeekphp olleH ?> |
Count number of words in a string
str_word_count () =>
str_word_count()
is used to get the number of words in a string.
1 2 3 4 5 6 7 |
<?php $string = "Hello phpkeeda"; echo str_word_count($string); // Output : 2 ?> |
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(“mymail@gmail.com”, “@”);
Suppose we want to get the position of @ symbol inside a string. We will write
1 2 3 4 5 6 7 8 9 10 11 |
<?php $string = "mymail@gmail.com"; echo strpos($string, "@"); // Output 6 // The first character count starts from 0 (not 1) ?> |
Convert String into lower case
strtolower() => strtolower() is used to converts the string into lowercase
1 2 3 4 5 6 |
<?php echo strtolower("HeLlo PhpKeEda"); //output : hello phpkeeda ?> |
Convert string into upper case
strtoupper() =>
strtoupper()
is used to convert string into uppercase.
1 2 3 4 5 |
<?php echo strtoupper("HeLlo PhpKeEda"); // Output : HELLO PHPKEEDA ?> |
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.
1 2 3 4 5 |
<?php echo ucfirst("hello phpkeeda"); // Output : Hello phpkeeda ?> |
Convert first letter of string to lower case
lcfirst() =>
lcfirst()
is used to convert string’s first letter to uppercase.
1 2 3 4 5 |
<?php echo lcfirst("Hello Phpkeeda"); // Output : hello phpkeeda ?> |
Convert each first letter of string to uppercase
ucwords() =>
ucwords()
is used to convert each first letter of given string to uppercase.
1 2 3 4 5 |
<?php echo ucwords("hello phpkeeda"); // Output : Hello Phpkeeda ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $string = "Hello phpkeeda"; echo $string . "<br>"; // this is the actual string // output Hello phpkeeda echo ltrim($str,"Hello!"); // after trim // output : phpkeeda ?> |
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.
1 2 3 4 5 6 7 8 9 10 |
<?php $str = "Hello phpkeeda"; echo $str . "<br>"; // this is the actual string // output Hello phpkeeda echo rtrim($str,"phpkeeda!"); // after trim // output : Hello ?> |