Strings in Php
Table of Contents.
Declaring Strings
String are variables in which we can store text and manipulate text.
Here is an example of how string is declared in php.
[php]
<?php
$txt = “Hello World!”;
echo $txt;
?>
[/php]
Output is
[php]
Hello World!
[/php]
The text string is always enclosed in double quotes (“). There is no need to specify the data type. There are many functions to manipulate strings in php. Only some of the important are discussed here.
Return to Top
Concatenating Strings
In php we concatenate strings using dot (.) .It is the operator for joining two texts.
Example how to concatenate strings.
[php]
<?php
$txt1 = “php tutorials”;
$txt2 = ” best site”;
echo $txt1.”hello world<br>”;
echo “This is to join”.” strings<br>”;
echo $txt1.$txt2;
?>
[/php]
Output
[php]
php tutorialshello world
This is to join strings
php tutorials best site
[/php]
Return to Top
Splitting Strings
Function explode is used to split strings in php. It takes two inputs one is with criteria the string must be split and the input string.
Example
[php]
<?php
$txt = “Programming is fun”;
$pieces = explode(” “,$txt);
echo $pieces[0].”<br>”;
echo $pieces[1].”<br>”;
echo $pieces[2].”<br>”;
?>
[/php]
Output
[php]
Programming
is
fun
[/php]
Return to Top
How to find Length of String
Function strlen() gives you the Length of string.
[php]
<?php
$txt =”Hello”;
echo strlen($txt);
?>
[/php]
Output
[php]
5
[/php]
Return to Top
How to Get Substring of String
The function substr() is used to get the substring of a string in php.
Usage is substr($sourcetext,start,length).
You have to give three arguments to the substr function.
1)$sourcetext — the text from which you want to get the substring
2)start — integer.. the no of character from which you want the string
3)length — how many character you want to pickup (this argument is optional).
Example
[php]
<?php
$txt = “Hello World”;
$string = substr($txt,7,4);
echo $string;
?>
[/php]
Output
[php]
Worl
[/php]
Second type of usage.
substr($sourcetext,start-index)
.
If you donot specify the legnth of substring to be selected the function automatically selects the substring starting from the start index to the end of the string.
Example
[php]
<?php
$text = “what are you doing”;
echo substr($text,6);
?>
[/php]
Output
[php]
re you doing
[/php]
Return to Top
Comparing two Strings in php
Use function strcmp to compare two strings in php.
Usage
[php]
strcmp($string1,$string2).
[/php]
Here we are comparting two texts $string1 and $string2.
The function returns zero if both of them are equal and non zero if both of them are not equal. This function is case sensitive.
If you are looking for case insensitive comparision you have to use function strcasecmp. The usage is same as that of strcmp function and it returns zero (0) if both strings are equal and non-zero values if they are not.
Return to Top
Search for a string in another string.
We use strpos for searching for one string in another string.
Usage
[php]
strpos($sourcestring,$texttobefound)
[/php]
Here $sourcestring is the string in which we are searching and $textobefound is the text for which we are searching for.
This function returns the postion of the first occurance of the string or text we are searching for. If the text is not found it returns boolean value FALSE.
Example
[php]
<?php
$source = “I am an Indian”;
$search1 = “am”;
$search2 = “noid”;
echo strpos($source,$search1);
echo “<br>”;
if(!strpos($source,$search2)){
echo “string is not present”;
}
echo strpos($source,”India”);
?>
[/php]
Output
[php]
2
string is not present
8
[/php]
Return to Top
Replacing Characters in a String
We use function str_replace to replace a sequence of characters in a string.
Usage is
[php]
str_replace($search,$replace,$string)
[/php]
Here $search is the sequence of characters for which we want to replace in the string. $replace is the sequence of characters with which we want to replace the $search. $string is the text in which we want to make the changes.
Example
[php]
<?php
echo str_replace(“i”,”x”,”Aerospace is one of growing industries”);
?>
[/php]
Output is
[php]
Aerospace xs one of growxng xndustrxes
[/php]
Here we are replacing ‘i’ with ‘x’. This replacement is case sensitive. If you are looking for case insensitive function use str_ireplace.
Return to Top
Functions to change Case
The following functions are genreally used to convert strings from one case to other.
All of the below functions returns the string after making appropriate changes. You must rewrite the original string value with the changed one like $stringtoconvert = ucwords($stringtoconvert)
[php]
ucwords($stringtoconvert)— Capitalizes first alphabet of every word in the string or text
ucfirst($stringtoconvert)— Capitalizes only first alphabet of string
strtolower($stringtoconvert)— Converts all alphabets to small.
strtoupper($stringtoconvert)— Capitalizes every alphabet in the string.
[/php]
Return to Top