PHP String Functions By Examples
\\ If you have any query or questions on my post, then please comment below //
String Function: As we know the string is a collection of characters or you can also say string is a sequence of characters.
Example Of String Function: "Technical Programming".
Here Technical Programming is a string and we can also call it as a characters (set of character).
Some String Functions Are:
1. Strlen() function:
The strlen() function used to count the length of a string, in characters. It returns the length count value. This is mainly used to count the length of any string, or to stop the loop when the last character counted.
Example Of Strlen() function:
------------------------------------------
<?php
echo strlen("Technical Programming");
?>
-------------------------------------------
Output will be: 21
2. String Concatenation function or operator:
This function or operator is used to concatenate 2 string values, using dot (.) operator.
Example Of Concatenation:
-------------------------------------------------
<?php
$var1="Technical Programming";
$var2="Campus Nikalo";
echo $var1 . " " . $var2;
?>
-------------------------------------------------
Output Will be: Technical Programming Campus Nikalo
3. Strpos() function:
This function is used to search any character or text in a string. If he found that specified character, Then it returns, The position value of the first search. If no match is found, it will return FALSE value.
Example Of Strpos Function:
-----------------------------------------------------------
<?php
echo strpos("Technical Programming","Tech");
?>
---------------------------------------------------------
Output will be: 0.
Here Technical Programming is our string, And in that string we have to find Tech character. But you see Tech character starts with zero position. So here result will be 0.
Now you run this program and check the output.
--------------------------------------------------------------
<?php
echo strpos("Technical Programming","ca");
?>
--------------------------------------------------------------
Output will be: 6.
Here Technical Programming is our string, And in that string we have to find ca character. But you see ca character is on 6th & 7th position. So here result will be 6.
Some Of The String Functions Are:
1. addslashes Function: It adds slashes to specify characters in a string.
2. echo: This is used to show outputs of one or more strings.
3. explode: It splits a string into multiple strings based on the specified delimiter.
4. print: It show the outputs of one or more strings.
5. str_replace: It changes certain occurrences of a string with a replacement string.
6. strstr: Returns all characters of a string after the first match.
7. substr: Retrieves a portion of the string.
8. trim: Strips out white spaces at both ends of a string.
I hope in this post you learn:
1. What is a string.
2. How to use string functions in PHP.
3. Types of strings with their use.
4. Examples Of String Functions.