How to put String in Array, Split by New Line in PHP


What is PHP?

PHP, which stands for Hypertext Preprocessor, is a popular server-side scripting language used for web development. It is designed to create dynamic and interactive web pages. PHP is embedded within HTML code and executed on the server, generating HTML output that is sent to the client's browser. With its simple and easy-to-learn syntax, PHP allows developers to build dynamic websites, handle form data, interact with databases, and perform various server-side tasks. It has a vast ecosystem of libraries and frameworks that enhance its functionality and enable developers to create robust and scalable web applications. PHP is widely supported by hosting providers, making it a preferred choice for web development projects.

How to put string in array and split by new line in PHP

Method 1

Using preg_split() function

The preg_split() function in PHP is used to split a string into an array of substrings based on a specified regular expression pattern. It allows you to break a string into smaller parts based on specific criteria. By leveraging the capabilities of preg_split(), you can efficiently split strings based on flexible patterns, enabling you to process and manipulate text data in a structured manner within your PHP applications.

Syntax

The syntax of the preg_split() function is as follows:

preg_split(pattern, string, limit = -1, flags = 0);
  • pattern: Specifies the regular expression pattern to use for splitting the string.

  • string: Specifies the input string that needs to be split.

  • limit (optional): Specifies the maximum number of elements to return in the resulting array. If set to a positive value, only that many elements will be returned. If set to 0, all possible elements will be returned. If set to a negative value (default), no limit is imposed.

  • flags (optional): Specifies any additional flags to modify the behavior of the preg_split() function.

Example

<?php

// A php program to separate string
// using preg_split() function

// String which to be converted
$str = "Pradeep Menon
Arjit
Kavitha deshmukh
John Peter
Peterson"; // This function converts the string $arr= preg_split ('/
/', $str); // print the information of array print_r($arr); ?>

Output

Array
(
[0] => Pradeep Menon
[1] => Arjit
[2] => Kavitha deshmukh
[3] => John Peter
[4] => Peterson
)

Explanation of code

In this code we used preg_split() function to separate a string into an array using the newline character as the delimiter.

The input string $str contains a list of names separated by newline characters. The preg_split() function is then used with the regular expression pattern /
/ to split the string at each occurrence of a newline character.

After executing the preg_split() function, the resulting array $arr holds the individual names as its elements. Finally, the print_r() function is used to display the contents of the array.

In summary, this code snippet takes a string with names separated by newlines, applies preg_split() to split it into an array using the newline character as the delimiter, and then prints the resulting array with the separated names.

Method 2

Using explode() function

The explode() function in PHP is used to split a string into an array of substrings based on a specified delimiter. It is a simpler alternative to the preg_split() function and is particularly useful when working with simple delimiters, such as single characters or fixed strings. The explode() function is commonly used in scenarios such as parsing CSV files, extracting individual words from a sentence, or breaking down a string based on specific delimiters. It provides a straightforward and efficient way to split strings into smaller components.

Syntax

The syntax of the explode() function is as follows:

explode(delimiter, string, limit = PHP_INT_MAX);
  • delimiter: Specifies the string used as the delimiter to split the input string.

  • string: Specifies the input string that needs to be split.

  • limit (optional): Specifies the maximum number of elements to return in the resulting array. If set, the resulting array will have at most limit elements.

Example

Here's an example of explode() function:

<?php

// PHP program to separate string
// using explode function

// String which to be converted
$str = "Pradeep Menon
Ajith Lakshman
John Peter
Peterson"; // Function to convert string to array $arr = explode("
", $str); // Print the information of array print_r($arr); ?>

Output

Array
(
[0] => Pradeep Menon
[1] => Ajith Lakshman
[2] => John Peter
[3] => Peterson
)

Explanation of code

The above PHP code snippet demonstrates the usage of the explode() function to split a string into an array using the newline character (
) as the delimiter.

The input string $str contains a list of names separated by newline characters. The explode() function is then used with the newline character (
) as the delimiter to split the string into an array of substrings. After executing the explode() function, the resulting array $arr contains the individual names as its elements. Finally, the print_r() function is used to display the contents of the array.

Conclusion

In conclusion, both preg_split() and explode() are PHP functions used to split strings into arrays based on specified delimiters.preg_split() offers the advantage of using regular expressions as delimiters, providing more flexibility and advanced pattern matching capabilities. It is ideal for complex splitting requirements where the delimiters can vary or follow a specific pattern.On the other hand, explode() is simpler and faster, suitable for straightforward splitting tasks using fixed delimiters such as characters or strings. It is a more efficient choice when dealing with simple and predictable delimiters.

Updated on: 31-Jul-2023

511 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements