PHP wordwrap() function is used for wrapping a string to a given number of characters. It will cut the string at the specified length and insert the string or character we have entered at that position.

Its Syntax will be :
wordwrap ( string , length , break character , cut mode );

  • First parameter will be the string to be wrapped
  • Second parameter specifies at which length wrap should occur.
  • Third parameter specified the character or string to be inserted at the specified length.
  • Fourth parameter is optional. This cut mode parameter specified the mode of cutting. When we specify this parameter as TRUE , cutting will occur at the middle of words too , it will strictly cut at the specified length. By default its value will be false , and cutting wont happen in the middle of a word.

You will get more clear picture about this wordwrap function after seeing the following sample PHP programs and its output

<?php
$text="This tutorial will show about wordwrap function";
echo wordwrap($text,5," * ")
?>

Its output will be :

This * tutorial * will * show * about * wordwrap * function

Here we can see that single words are not splitted , they remains the same.

Now look at the next program with CUT mode parameter as TRUE

<?php
$text="This tutorial will show about wordwrap function";
echo wordwrap($text,5," * ", TRUE)
?>
Its output will be :

This * tutor * ial * will * show * about * wordw * rap * funct * ion

Here we can see that function has splitted the string strictly at the specified length and single words also splitted .

PHP html_entity_decode function is used for covert the HTML entities to its corresponding characters.This function can be used for decode the string maid by htmlentities function.

Its Syntax Will be :
html_entity_decode ( String , Quote Style, Character Set ) ;
First parameter is the string to be converted to characters
Second paramters is an optional will , which will specify how to deal with Single and double quotes
Available quote styles are
ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.

Third parameter is also optional, where we can specify the character set used in conversion

See a Sample PHP program using html_entity_decode function

<?php
$data="mysqlbrain's tutorials are very <b>good</b>";
$data_1 = htmlentities($data,ENT_QUOTES);
echo html_entity_decode($data_1);
echo "<br>";
echo html_entity_decode($data_1,ENT_QUOTES);
?>

Its output will be :

mysqlbrain’s tutorials are very good
mysqlbrain’s tutorials are very good

We can see that in html_entity_decode with ENT_QUOTES parameter convert the single quote html entity to character .

PHP htmlentities function is used for covert the characters to its HTML entities. This function will automatically identify the characters which have an equivalent HTML entity , and then convert to it.

Its Synatx will be ;
htmlentities ( string , quote style , Character set );

First parametrer will be the string to convert
Second parameter is optional. Here we can specify how to deal with single quotes (‘) and double quotes (“)
Available quote styles are

  • ENT_COMPAT : Will convert double-quotes and leave single-quotes alone.
  • ENT_QUOTES : Will convert both double and single quotes.
  • ENT_NOQUOTES : Will leave both double and single quotes unconverted.

Third parameter is also optional , which defines the character set used in conversion. By default character set will be ISO-8859-1

Now look at some sample PHP programs using htmlentities function

<?php
$data="mysqlbrain's tutorials are very <b>good<b>";
echo htmlentities($data);
echo "<br>";
echo htmlentities($data,ENT_QUOTES)
?>

Now look at the source code of the ouput in browser , it will be

mysqlbrain’s tutorials are very <b>good<b>
mysqlbrain’s tutorials are very <b>good<b>

We can see that first htmlentities statement leaves the single quote , and second time it covert the single quote to html entity.

Its a good practise to use this function before storing the data to mysql tables. And we can use PHP html_entity_decode function to decode the string.

PHP explode() function is used for split a string by another string into an array.This function is used when we need to split a string using a specific character or string present in that string.
Its Syntax will be :
explode (delimiter string , string to explode , LIMIT );

Here delimiter string will declare at which string occurrence the split process should happen . And LIMIT is an optional parameter for specifying the maximum count of splitting. If we specify LIMIT parameter , maximum number of splitting will be the LIMIT value and the rest of string will be the last element in array.

We will get more clear picture about explode() function after seeing some examples.

Sample PHP program using explode() function

<?php
$data="How to split a string using explode";
$splittedstring=explode(" ",$data);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>";
}
?>

Its output will be :

splittedstring[0] = How
splittedstring[1] = to
splittedstring[2] = split
splittedstring[3] = a
splittedstring[4] = string
splittedstring[5] = using
splittedstring[6] = explode

Here , we are trying to split the string at blank spaces with out optional parameter LIMIT value. And we can see that string is split at the positions where blank space occurred and stored in the array $splittedstring. We can retrieve the values splittedstring[0] for first split string and splittedstring[1] for the second part etc..
Here in this example I am looping through the array and output all the results.

Now look at a sample program with optional parameter LIMIT count with explode()

<?php
$data="How to split a string using explode";
$splittedstring=explode(" ",$data,3);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>";
}
?>

Its output will be :

splittedstring[0] = How
splittedstring[1] = to
splittedstring[2] = split a string using explode

Here we can see that optional parameter is specified as 3 as limit count. So the string is split to three parts and last part contain the rest of the original string.

Now look at the next sample PHP program with explode() function

<?php
$data="http://www.youtube.com/watch?v=ZR0iBvof3MA";
$splittedstring=explode("?v",$data);
echo "Video ID is ".$splittedstring[1];
?>

Output will be :

Video ID is =ZR0iBvof3MA

Here we trying to split a youtube video url by a string ?v for getting its video id.

Optional parameter with negative value:
If we specified -LIMIT , then the last LIMIT number of splitted results wont return.
See the flowing example and its output for understand whats happening

<?php
$data="ONE TWO THREE FOUR FIVE";
$splittedstring=explode(" ",$data,-2);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>";
}
?>

Its output will be :

splittedstring[0] = ONE
splittedstring[1] = TWO
splittedstring[2] = THREE

Here we can see that it wont store the last two components ( FOUR and FIVE ) in array.

PHP trim() function is used for removing the white space or other characters from starting and end of a string. Its used for formatting data properly. In some cases unnecessary whitespaces may be there in the beginning or end of a string, we can use this function to remove those blank spaces.

trim() function has an optional parameter for specifying the character to remove.
Its Syntax will be :
trim(string to be trimmed , character to remove );


By default it will remove the following character from the beginning and end of a string

  • ” ” :  an ordinarywhite space.
  • “\t” : a tab.
  • “\n” :  a new line.
  • “\r” : a carriage return.
  • “\0″ : the NUL-byte.
  • “\x0B” :  a vertical tab.

See the sample PHP program using trim function without parameter

<?php
$data=" how to remove blank spaces  ";
$data1=trim($data);
echo $data.":<br>";
echo $data1.":<br>";
?>

Now check the display result source code in browser , we can see

 how to remove blank spaces  :<br>how to remove blank spaces:<br>

Here we can see that $data contain whitespaces at the starting and end , and $data1 doesn’t contain those things ( our trim function has removed that )…

By default output in browser we wont feel any difference between these data because browsers will automatically trim the data for output . Thats why we need to check the source code in browser for the real values.

Now we can write another program using trim function with parameter

<?php
$data="how to remove blank spaces how";
$data1=trim($data,"how");
echo $data.":<br>";
echo $data1.":<br>";
?>

Its output will be :

how to remove blank spaces how:<br> to remove blank spaces :<br>

Here we have specified “how” as optional parameter with trim function and “how” is removed from the beginning and end of the string.

There is two more trim function available
ltrim() : Left trim only; Will remove only from the beginning of a string ( starting )
rtrim() : Right trim only will remove only from the end of a string

Rest everything is same as trim() function.

See the sample php program with ltrim() function

<?php
$data="how to remove blank spaces how";
$data1=ltrim($data,"how");
echo $data.":<br>";
echo $data1.":<br>";
?>

Its output will be :

how to remove blank spaces how:<br> to remove blank spaces how:<br>

Here we can see that “how” is removed from the beginning of string and “how” at end remains

Now look at the program with rtrim()

<?php
$data="how to remove blank spaces how";
$data1=rtrim($data,"how");
echo $data.":<br>";
echo $data1.":<br>";
?>

Its output will be L

how to remove blank spaces how:<br>how to remove blank spaces :<br>

Here we can see that “how” is removed from end only, the beginning “how” remains in the ouput string .

We can use any of these function for remove the un necessary whitespaces or characters from the end or beginning or from both as per our requirement.
And note trim() = rtrim() + ltrim() in action.

PHP strpos function is using for checking the occurrence position of a character or string within a string.It will return the numeric position number of the first occurrence of the searched string.
If strpos function failed to find the searched string , it will return “FLASE”.

For example if we want to know the position of letter “M” in word “MALAYALAM” , strpos function will return the value 0 ( zero ) . It means the searched letter’s position is first on that string.
Please note : Note count is starting from 0 (zero) not from 1 (one )

See the following sample PHP program with strpos function

<?php
$main_string="MALAYALAM";
$search_string="M";
$position  = strpos($main_string, $search_string);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

The output of this PHP script will be :
found at position 0

See another example

<?php
$main_string="Hello world";
$search_string="world";
$position  = strpos($main_string, $search_string);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

The output of this PHP script will be :
found at position 6

Ok. Now we got the idea about how to get a string’s first occurrence position in another string. But in some situations we need to get the first occurrence position with a offset. It means if need to search the occurrence of a string from a fixed position of the main string than from its starting. ( like we need to start the search from position 3 and avoid first 4 position of the main string ).
You will get a clear picture after seeing the following sample program

<?php
$main_string="MALAYALAM";
$search_string="M";
$position  = strpos($main_string, $search_string, 1);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

Output of this program will be
found at position 8

By using the optional offset parameter 1 with strpos function , search will ignore anything before the specified offset value 1 in the main string. So in this example the first M in string MALAYALAM wont consider because it is before the specified offset value 1.

While our PHP programming , in some cases we need to get the length of a string for processing. strlen() if the function in PHP to get the length of a string.

See the sample program with strlen() function:

<?php
$variable="Check length";
echo strlen($variable);
?>

It will output the result

12

strlen function will count the blank spaces also contains in the string.

For looping through a string, we can use this function to know the end of loop by storing the length of string to a variable. And in other cases we can use this function for data checking like password length and all.

It will return 0 ( zero ) if the string is empty.

Concatenation is the process of joining multiple strings into one.In PHP concatenate  strings is doing by concatenation operator ( “.” dot symbol ).Using of concatenation is highly used one in PHP programs. We need to use this for string concatenations and for printing the results in desired order etc.

Here is one Sample PHP program with Concatenate strings  using concatenation operator

<?php
$variable_1 =  "Syam";
$variable_2 = "is a PHP professional";
$variable_3 = "From India";
echo $variable_1." ".$variable_2." ".$variable_3;
?>

Output of this PHP program will look like
Syam is a PHP professional From India

In Some other cases we need to store the concatenated strings as a variable

See the following code

<?php
$variable_1 =  "Syam";
$variable_2 = "is a PHP professional";
$variable_3 = "From India";
$variable_4 = $variable_1." ".$variable_2." ".$variable_3;
echo $variable_4;
?>

In this code concatenated string is stored in the variable “$variable_4″  for later use in the program. Above code will ouput the same result as our previous example.