PHP String Functions

What are String in PHP?

A string is a Collection of characters Putting together. In PHP string can be a collection of numbers or alphabets nor a mixture of number and alphabets.String is Enclosed with in the ” “. 

In PHP string has a wide role to play ,You can work on various way with the string in PHP .

Where the String Stored ?

String Store in variable like $str= “Name of book”;

There are various way to deal with string.Some important and useful  string  function of PHP are give below

Assume $str=”PHP is Simple”;

How to count the length of string ?

$l=strlen($str);

echo”Number of charater in Variable string  $str are $l”;

Output:13

How to convert string into upper case?

$stu=strtoupper(#str);

echo $stu;

Output:PHP IS SIMPLE

How to convert string into lower case?

$stl=strtolower(#str);

echo $stl;

Output:php is simple

How to Copy string?

$cpy=$str;

echo $cpy;

Output:PHP is Simple

How to merge Two string?

$addstr=”and easy”;

$merge= $str .$addstr;

echo $merge;

Output:PHP is Simple and easy

How to reverse a string ?

$a=hello;

$b=strrev($a);

echo $b;

Output: olleh

How to Break A string ?

$char=” “;

$result = strpbrk($str, $char);

echo $result;

Output: PHP

Note :this function breaks a string from specified charter if found ,it search for the first instance of character in string , in above example it is space .

How to Replace Text Within a String ?

$newstr =str_replace(“Simple”, “easy”,$str);

Outputs: PHP is easy;

 

One thought on “PHP String Functions

Leave a Reply