Home » PHP » PHP Examples

PHP | Split comma delimited string into an array without using library function

Splitting string in PHP: Here, we are going to learn how to split comma delimited string into an array without using library function in PHP?
Submitted by Bhanu Sharma, on August 11, 2019

Given a string with comma delimited, we have to split it into an array.

Example:

    Input: 
    "Google,Bing,Yahoo!,DuckDuckGo"

    Output:
    arrar of strings after splitting...
    Array
    (
        [0] => Google   
        [1] => Bing     
        [2] => Yahoo!   
        [3] => DuckDuckGo
    )

PHP code to split comma delimited string into an array without using library function

<?php
//PHP code to reverse the string without 
//using library function

//function definition 
//it accepts a string and returns an array 
//delimited by commas
function split_string($text){
    //variable to store the result i.e. an array 
    $arr = [];
    //calculate string length
    $strLength = strlen($text);
    $dl = ','; //delimeter
    $j = 0;
    $tmp = ''; //a temp variable
    //logic - it will check all characters
    //and split the string when comma found
    for ($i = 0; $i < $strLength; $i++) {
    	if($dl === $text[$i]) {
    		$j++;
    		$tmp = '';
    		continue;
    	}
    	$tmp .= $text[$i];
    	$arr[$j] = $tmp;
    }
    //return the result
    return $arr;
}

//main code i.e. function calling
$str = "New Delhi,Mumbai,Chennai,Banglore";
$result = split_string($str);
echo "string is: " .$str. "<br/>";
echo "arrar of strings after splitting..."."<br/>";
print_r($result);

$str = "Google,Bing,Yahoo!,DuckDuckGo";
$result = split_string($str);
echo "string is: " .$str. "<br/>";
echo "arrar of strings after splitting..."."<br/>";
print_r($result);
?>

Output

string is: New Delhi,Mumbai,Chennai,Banglore
arrar of strings after splitting...
Array
(
    [0] => New Delhi
    [1] => Mumbai
    [2] => Chennai
    [3] => Banglore
)
string is: Google,Bing,Yahoo!,DuckDuckGo
arrar of strings after splitting...
Array
(
    [0] => Google   
    [1] => Bing     
    [2] => Yahoo!   
    [3] => DuckDuckGo
)   

Explanation:

We use a for loop to convert our comma delimited string into an array. We identify when a (,) appears in the string and copy that into an array then follow this process until the whole length of the string is covered. The inverted string is stored into a temporary variable ($tmp) then moved to an array ($arr[]).







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.