In this tutorial, learn about PHP strings and method to find the length, count the number of characters, replace text, reverse string, concatenate strings, and split a string into an array using PHP.
What is PHP String
PHP string is a sequence of characters that may contain letters, numbers, and special characters. You can create a string in PHP by placing the characters within the single or double quotes as given below:
1 |
$myStr = "Welcome to TutorialDeep!"; |
Get Length of Strings in PHP
The length is the number of characters present in the string including spaces. To find the length of the string in PHP, you have to use the strlen()
as given below:
1 2 3 4 |
<?php $myStr = "Welcome to TutorialDeep!"; echo strlen($myStr); // output: 24 ?> |
Output
The above example shows that the length of the string is 24.
Count Number of Words in a Strings in PHP
To count the number of words in a string, you have to use the str_word_count()
function of PHP and pass string variable as an argument.
1 2 3 4 |
<?php $myStr = "Welcome to TutorialDeep!"; echo str_word_count($myStr); // output: 3 ?> |
Output
The output shows that there are 3 words in a string above.
Replace Text Content in a String Using PHP
You can replace the text content in a string with the new text of your choice. To replace the text content of a string, you have to use the str_replace()
of PHP as given below:
1 2 3 4 |
<?php $myStr = "Learn with TutorialDeep!"; echo str_replace("Learn", "PHP", $myStr); ?> |
Output
The above example replaces “Learn” with “PHP”.
Reverse String
You can reverse the sequence of characters in a string. To reverse the string in PHP, you have to use the strrev()
and pass the string variable as an argument.
1 2 3 4 |
<?php $myStr = "Learn with TutorialDeep!"; echo strrev($myStr); ?> |
Output
The last character comes first after reversing the order of characters in a string.
Concatenate PHP Strings
Concatenation of PHP strings is the process of adding two or more strings together into a single string. To concatenate strings in PHP, you have to use the concatenation (.) operator as given below:
1 2 3 4 5 |
<?php $myStr1 = "Hello "; $myStr2 = "World!"; echo $myStr1.$myStr2; // output: Hello World! ?> |
Output
The result of concatenation joins the two strings into a single string.
Search For a Text If String Contains
1 2 3 4 5 6 |
You can search for a text content if present in the PHP string. To search for the text content, you have to use the <code>strpos()</code> function of PHP. It takes two arguments in which the first argument is the string variable and second is the text to search. After it gets the matching character, it return its character position of the first match as given below: <?php $myStr = "Welcome to TutorialDeep!"; echo strpos($myStr, "TutorialDeep"); // output: 11 ?> |
Output
The above example shows that the character position of the first match is 11.
Split String into an Array in PHP
Split a string into an array is the process of breaking the string into an array in PHP. To break the string into an array, you have to use the explode()
function of PHP as given below:
1 2 3 4 |
<?php $myStr = "Welcome to TutorialDeep!"; print_r(explode(" ",$myStr)); ?> |
Output
You May Also Like to Read