What is string in php?
String variables are used for values that contain characters. A
string can be used directly in a function or it can be stored in
a variable. String value are enclosed within single or double
quotes marks.
Example.
<?php
$str = "Hello";
$ss = 'hi';
echo $str;
echo $ss;
?>
Output :-
Difference between single ' ' quotation and double " "
quotation. :-
singly quoted strings are treated almost literally, whereas
doubly quoted string replace variable with their values as well
as specially interpreting certain character sequence.
<?php
$var = "mahtab";
$str = 'my name is $var';
print ($str);
$ss = "my name is $var";
print($ss);
?>
Output :-
my name is $var
my name is mahtab
Concatenation operator :-
There is only one string operator in php. The concatenation
operator (.) is used to join two string values together.
<?php
$str1 = "my name is ";
$str2 = "mahtab";
echo $str1. " " .$str2;
?>
Output :-
Example :-
<?php
$x = 5;
$y = star;
$add = $x.$y;
?>
Output :- 5star
strlen() function :-
It return the length of the string in characters.
<?php
$str = "mahtab";
echo $str;
$len =
strlen($str);
echo $len;
echo strlen("hello");
?>
6 5
strops() function :-
The strops( ) function is used to search for a character or
specific text within a string. If a match is found, it will
return the character position of the first match. If no match is
found it will return false.
<?php
echo strops("hello mahtab", "mahtab");
?>
Output :- 6
Comparing string :-
There are many ways in which two strings can be compared for equality.
strcmp()
strcasecmp( )
strspn( )
strcspn( )
The strcmp( ) function performs a binary-safe case-sensitive comparison of two string.
int strcmp(String str1, String str2) :- it will return one of the three possible values.
0 :- if str1 and str2 are equal.
-1 :- if str1 is less than str2.
1 :- if str1 is greater than str2.
<?php
$p1 = "password"; $p2 = "password2";
if(strcmp($p1, $p2) != 0)
{
echo "password don't match";
}
else
{
echo "password match";
}
?>
Case insensitivity :-
The strcasecmp( ) function operates exactly like strcmp( ), except that is comparison is case insensitive.
Int strcasecmp(String s1, String s2)
<?php
$pwd1 = "mahtab"; $pwd2 = "MAHTAB";
if(! strcasecmp($pwd1, $pwd2) )
{
echo "password don't match";
}
else
{
echo "password match";
}
?>
Similiarity between Two Strings :-
The strspn( ) function returns the length of the first segment in a string containing characters also found in another strings.
<?php
$pwd = "12345678";
If(strspn ($pwd, "12345") == strlen($pwd))
Echo "passward doesn't match";
?>
String Functions :-
trim() :-
trim function remove white space from the the beginning and end of a string.
Syntax :-
trim(string,charlist)
here string is the user defined string.
charlist is the optional.
Specifies which characters to remove from the string. If omitted, all of the following characters are removed:
"\0" - NULL
"\t" - tab
"\n" - new line
"\x0B" - vertical tab
"\r" - carriage return
" " - ordinary white space
<?php
$str = " itech tuto ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
<?php
$str = "\n\n\nHello itechtuto!\n\n\n";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
Output :-
Without trim:
Hello World!
With trim: Hello World!
Other Function
ltrim() - Removes whitespace or other predefined characters from the beginning of a string
rtrim() - Removes whitespace or other predefined characters from the end of a string
Count The Number of Words in a String
The PHP str_word_count() function counts the number of words in a string:
<?php
echo str_word_count("Hello itechtuto");
?>
Output :-
Reverse a String
in PHP, strrev() function reverses a string:
<?php
echo strrev("Hello itechtuto");
?>
Output :-
Replace Text Within a String
str_replace() function replaces some characters with some other characters in a string.
<?php
echo str_replace("gmaxlifesciences", "itechtuto", "Hello gmaxlifesciences!");
?>
Output :-
Previous
Next