String is an array of characters. You can create a string simpy with the use of
'' and the content between quotes. Here are some strings.
<?php $str = "This is my first string"; echo $str."<br>"; $str_num = "You can have number as well 1232"; $string = "13212"; echo $str_num."<br>"; echo $string; ?>
You can have number as well 1232
We have used
. operator so many times while printing. This operator is to concatenate two strings. You can simply add two string using
There are lots of function for manipulating strings and some of those are :,
- strlen : Gives the length of the string passed
- str_replace :
str_replace($a,$b,$str)Will replace all the occurencs of
- trim : Will remove whitespaces from the beginning and end of the string. You can have custom characters also removed from the beginning and end of the string. This can be done using
$chris the character that need to be trimmed(can also be a string)
- strtolower : Converts the whole string to lower case
- strtoupper : Make the whole string to upper case
- methaphone : Converts a string to its metaphone equivalent. This function is very useful when you want to create a search engine that autocorrects to some extent.
- htmlspecialchars : Converts special characters (i.e. <,>," etc.) to htmlentities.
For those of you who are not aware about html entites, just think a way to show a tag in text(For example bold tag : <b>). Your browser will read this as html tag and it will never be shown in text. To overcome this problem we convert these special characters to html entities and these will be considered as plain text not tags.
<?php $str = "This is my first string"; // Length of string echo strlen($str)."<br>"; // replacing my with your echo str_replace("my","your",$str)."<br>"; // trimming Th echo trim($str,"Th")."<br>"; // Converting to lower case echo strtolower($str)."<br>"; // Metaphonee equivalent echo metaphone($str); // Note that none of these make changes to the original variable // They return manipulated string which you can use or store ?>
This is your first string
is is my first string
this is my first string