How do you split a string into an array?
PHP explode() function is used to split the string into an array based on a certain character, substring or a symbol.
In this example, we have a string containing language list separated by a comma. Therefore, we can pass a comma character as a delimiter to break the string into the array.
<?php
// Take a string containing language list separated by a comma
$string = "PHP, HTML, CSS, JavaScript, MySQL";
// Use PHP explode function to extract all names separated by comma
$name_array = explode(",", $string);
// print array
print_r($name_array);
?>
Run example: https://paiza.io/projects/fXWTx9TVtj1FFvQPw75Wyg
Tutorials Class - Output Window
Array
(
[0] => PHP
[1] => HTML
[2] => CSS
[3] => JavaScript
[4] => MySQL
)
Learn more about the similar topics:
Tutorials |
---|
No Content Found. |
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |