Comma Separated String To Array PHP
Last Updated : Mar 11, 2024
In this article we will show you the solution of comma separated string to array PHP, we will use the explode () function to comma-separated string to an array. Let us understand the explode () function first.
explode (): The explode() function breaks a string into an array. Explode is used to split a string using another string.
Step By Step Guide On Comma Separated String To Array PHP :-
<!DOCTYPE html> <html lang = " en " > <head> <meta charset = " UTF - 8" > <meta http-equiv = " X-UA-Compatible " content = " IE=edge " > <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " > <title> comma separated string to array php </title> <style> body { font-family : 'Lucida Sans', Verdana , sans-serif ; } h1 { font-size : larger ; font-weight : bolder ; color : rgb(113, 221, 113) ; text-align : center ; } h3 { text-align : center ; } </style> </head> <body> <h1> TALKERSCODE </h1> <h3> comma separated string to array php </h3> </body> </html> <?php $string = "Aa, Bb, Cc, Dd, Ee, Ff" ; echo "<br>" ; echo $string ; $split = explode (',', $string) ; echo "<br>" ; print_r ($split) ; for ($i=0; $i<sizeof ($split); $i++) { echo $split [$i] ; echo "<br>" ; } ?>
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- <style> tag to add CSS to the HTML page
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Close the HTML tag with </html>
- At first <?php tag to write the php.
- Creating a string within the variable $string.
- Using echo to display the string
- Now create another variable $split to add the explode() function
- Using the print_r () to display the variable after comma separated
- We also can use a for loop for looping through the array and displaying each variable using echo.
- <br> used here to add spacing
- Close the php code with ?> tag
Conclusion :-
Last, here in conclusion, here we can say that with this article’s help, we know how to comma separated strings into arrays using PHP.
I hope this article on comma separated string to array PHP helps you and the steps and method mentioned above are easy to follow and implement.