How to put string in array, split by new line in PHP ? Last Updated : 24 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Given a string concatenated with several new line character. The task is to split that string and store them into array such that strings are splitted by the newline.Example: Input : string is 'Ankit \n Ram \n Shyam' Output : Array ( [0] => Ankit [1] => Ram [2] => Shyam ) Using explode() Function: The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string. This function accepts separator and string to be separated and optional argument length of string to be separated.Example: php <?php // PHP program to separate string // using explode function // String which to be converted $str = "Ankit Mishra\nRam Singh\nShyam Pandey"; // Function to convert string to array $arr = explode("\n", $str); // Print the information of array print_r($arr); ?> Output: Array ( [0] => Ankit Mishra [1] => Ram Singh [2] => Shyam Pandey ) Using preg_split() Function: The function splits the string into smaller strings or sub-strings of length which is specified by the user. If the limit is specified then small string or sub-strings up to limit return through an array. The preg_split() function is similar to explode() function but the difference is used to the regular expression to specify the delimiter but explode is not used it. This function takes first argument as regular expression which is used for splitting second argument is as string which is to be splitted.Example: php <?php // A php program to separate string // using preg_split() function // String which to be converted $str = "Ankit Mishra\nRam Singh\nShyam Pandey"; // This function converts the string $arr= preg_split ('/\n/', $str); // print the information of array print_r($arr); ?> Output: Array ( [0] => Ankit Mishra [1] => Ram Singh [2] => Shyam Pandey ) Comment More infoAdvertise with us Next Article How to put string in array, split by new line in PHP ? ankit15697 Follow Improve Article Tags : Web Technologies PHP PHP Programs Web-Programs Similar Reads Split a String into an Array of Words in PHP In PHP, splitting a string into an array involves breaking a string into smaller parts based on a delimiter or a pattern and storing those parts as elements of an array. PHP provides several methods to achieve this, making it easy to convert a string into an array of words:Table of ContentUsing expl 3 min read How to Split Array into Specific Number of Chunks in PHP ? This article will show you how to split an array into a specific number of chunks using PHP. There are basically two approaches to solve this problem, these are:Table of ContentUsing array_chunk() functionUsing array_slice() functionUsing array_map with rangeUsing a Generator FunctionUsing a While L 5 min read PHP Program to Split & Join a String Given a String, the task is to split and join strings in PHP. It is a common operations that allow you to manipulate and manage text data effectively. Below are the approaches to split and join a string in PHP: Table of Content Using explode() and implode() FunctionUsing str_split() and Custom Join 3 min read How to Split String by Delimiter/Separator in PHP? Splitting a string by a delimiter in PHP is a common task that involves breaking a string into smaller components based on a specific character or pattern. This technique is essential for processing and manipulating strings in various applications, such as parsing CSV data or handling user inputs.Ex 4 min read How to Insert Characters in a String at a Certain Position in PHP ? You will learn how to insert characters into a string at a specific position in PHP. Given a string str and an integer position that describes the index in the original string where the desired string insertStr will be added. This can be achieved using various approaches, such as string concatenatio 3 min read How to trim all strings in an array in PHP ? Given a string array with white spaces, the task is to remove all white spaces from every object of an array.Examples: Input: arr = ["Geeks ", " for", " Geeks "] Output: Geeks for Geeks Method 1: Using trim() and array_walk() function: trim() Function: The trim() function is an inbuilt function whic 3 min read Program to Insert new item in array on any position in PHP New item in an array can be inserted with the help of array_splice() function of PHP. This function removes a portion of an array and replaces it with something else. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specifi 4 min read How to Convert Byte Array to String in PHP? Given a Byte Array, the task is to convert the byte array to a String in PHP. It is used in various scenarios, such as processing binary data, handling file uploads, or working with data transmission protocols. Below are the approaches to convert byte array to string in PHP:Table of ContentWhat is a 3 min read How to prepend a string in PHP? We have given two strings and the task is to prepend a string str1 with another string str2 in PHP. There is no specific function to prepend a string in PHP. To do this task, we have the following operators in PHP:Below are the methods to prepend a string in PHPTable of ContentUsing Concatenation Op 2 min read How to remove line breaks from the string in PHP? The line break can be removed from string by using str_replace() function. The str_replace() function is an inbuilt function in PHP which is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string o 2 min read Like