Similarly, you can use space, commas or any other words as a $delimiter. Like in the above example, we used “–“ as a delimiter to break the phone numbers. The $delimiter specifies the boundary string.The $array_name is an array as such explode method returns an array of split strings.See the following part for description of each parameter: specifying the number of splits or limiting the string pieces. Out of those, the last is an optional parameter i.e. The Third item = 567678 How to use explode function of PHPĪs shown in the first part of this guide, you may specify three parameters in PHP explode function. The explode method will break this into the following array elements since explode returns an array of broken strings or substrings. In that case, you can use the explode PHP method to break that phone number by using dash or hyphen as a delimiter, after taking this as input in string format. While you need to separate the country and area code that are separated by dashes. For example, your web form is taking the phone number with country and area code in this format: The explode method is also searched as “PHP split” or split a string, which is used to split the given strings. The total count of the array element is also 5 rather 10 as in the above example. You can see the demo page, only five substrings are displayed while the last substring is the remaining substring of the given string. This time, we specified 5 splits by using the limit parameter. “This is explode tutorial that enables string split in PHP”Īs it returned total of ten substrings while a space delimiter was used. This is the same example as above except we used the limit parameter to specify the number of splits in the string. See online demo and code An example with the limit parameter See the example and output by the image or link below: A foreach loop to display the returned array elements is used.įinally, we used the count function to display the total number of substrings or elements in that array. After creating the string, the PHP explode method is used to split that string. This is explode tutorial that enables string split in PHPĪgain, we did not use the limit parameter in that example. In this example, we used the space delimiter. See online demo and code An example with space delimiter See the demo and code by clicking the following image or link: As there is no limit parameter, the whole phone number will be broken in three substrings. The returned array is assigned to an array.įinally, we used a PHP foreach loop to display the array element values that are split substrings of the phone number string. In the following example, we have declared a string variable and assigned it a phone number in this format:Īfter that, the explode method is used to split a string by using the hyphen (dash) delimiter. $array_name = explode ( $delimiter, $string_to_split, $no_of_breaks )Īn explode example to split phone numbers
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |