PHP String to Array: In this tutorial you will come to know about conversion of string to an array using explode() function. It includes general format,parameters we have to pass and the value it returns. Return value could be different which is based on its third parameter. Several examples will help you to understand the complexity of the third parameter of explode() function.
PHP String to Array: In this tutorial you will come to know about conversion of string to an array using explode() function. It includes general format,parameters we have to pass and the value it returns. Return value could be different which is based on its third parameter. Several examples will help you to understand the complexity of the third parameter of explode() function.PHP String to Array
PHP provides many functions to break a single string into smaller components or words, like: explode(), sscanf() etc. Sometimes data arrive in such a way that we have to break the string into an array. For example sometimes we get the details of an employee in a single array, like: "Emp01,Varun,New Delhi", as this example clearly illustrates that an employee's name is Varun, his employee id is Emp01 and he lives in New Delhi. So if we want to store this data into database we need to break this into an array first. At this time we need to use explode() function.
Small description of explode is as follows:
General Format | array explode (string $separator, string $string [,int limit]) | |||
Parameters | $separator: Any separator, any special symbol (in general) | $string: input string | Limit could be:
This parameter is discussed in the follwing examples |
|
Return Value | If the separator/delimiter is: | It will return: | ||
|
|
|||
|
|
|||
|
|
Example 1 (Using no third parameter):
<?php $string='India,Pakistan,Sri Lanka, Bangladesh'; echo"
Value of the string is:
"; print_r($string); echo"
After exploding the string values of an array is:
"; $country=explode(',',$string); print_r($country); echo "
One of India's neighbouring country is:$country[2]
"; ?>
Output:
Value of the string is: India,Pakistan,Sri Lanka, Bangladesh After exploding the string values of an array is: Array ( [0] => India [1] => Pakistan [2] => Sri Lanka [3] => Bangladesh ) One of India's neighbouring country is:Sri Lanka
Example 2 (Using third parameter with a positive value)
<?php
$string='India,Pakistan,Sri Lanka, Bangladesh';
echo"
Value of the string is:
";
print_r($string);
echo"
After exploding the string values of an array is:
";
$country=explode(',',$string,3);
print_r($country);
?>
Output:
Value of the string is: India,Pakistan,Sri Lanka, Bangladesh After exploding the string values of an array is: Array ( [0] => India [1] => Pakistan [2] => Sri Lanka, Bangladesh )
Example 3 (Using third parameter with a negative value)
<?php $string='India,Pakistan,Sri Lanka, Bangladesh'; echo"
Value of the string is:
"; print_r($string); echo"
After exploding the string values of an array is:
"; $country=explode(',',$string,-2); print_r($country); ?>
Output:
Value of the string is: India,Pakistan,Sri Lanka, Bangladesh After exploding the string values of an array is: Array ( [0] => India [1] => Pakistan )
Example 4 (Using third parameter with zero)
<?php $string='India,Pakistan,Sri Lanka, Bangladesh'; echo"
Value of the string is:
"; print_r($string); echo"
After exploding the string values of an array is:
"; $country=explode(',',$string,0); print_r($country); ?>
Output:
Value of the string is: India,Pakistan,Sri Lanka, Bangladesh After exploding the string values of an array is: Array ( [0] => India,Pakistan,Sri Lanka, Bangladesh )
Note: The output of the above program will be equal if we use 1 instead of 0 in the place of third parameter.