Change language

How to put a string into an array, split on a newline in PHP?

|
Example :
Input: string is ’Ankit Ram Shyam’ Output: Array ([0] = > Ankit [1] = > Ram [2] = > Shyam) 
Using the explode() function: The explode ( ) splits the string based on the line separator, that is, splits the string wherever the separator character occurs. This function returns an array containing strings formed by splitting the original string. This function takes a delimiter and string to delimit and an optional string length argument to delimit.Example :  
// PHP line splitter
// using the explode function 
// The string to convert $str = "Ankit Mishra Ram Singh Shyam Pandey" ;  
// Function for converting a string to an array $arr = explode ( " " , $str );  
// Print array information print_r ( $arr ); ?>
Exit:
Array ([ 0] = > Ankit Mishra [1] = > Ram Singh [2] = > Shyam Pandey) 
Using the preg_split() function: The function splits a string into smaller strings or substrings of length specified by the user. If a limit is specified, then the small string or substrings are incremented to the limit returned through the array. The preg_split() function is similar to the explode() function, but the difference is used in the regular expression to specify the separator, but it is not used in explode. This function takes the first argument as a regular expression, which is used to split the second argument as a string to be split.Example :  
// PHP program for separating a line
// using the preg_split() function 
// The string to be converted $str = " Ankit Mishra Ram Singh Shyam Pandey " ;  
// This function converts the string $arr = preg_split ( ’/ /’ , $str );  
// display information from the array print_r ( $arr ); ?>
Exit:
Array ([ 0] = > Ankit Mishra [1] = > Ram Singh [2] = > Shyam Pandey) 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically