How to split a string into an array in PHP
Topic: PHP / MySQLPrev|Next
Answer: Use the PHP explode()
function
You can use the PHP explode()
function to split or break a string into an array by a separator string like space, comma, hyphen etc. You can also set the optional limit parameter to specify the number of array elements to return. Let's try out an example and see how it works:
Example
Run this code »<?php
$my_str = 'The quick brown fox jumps over the lazy dog.';
print_r(explode(" ", $my_str));
echo "<br>";
print_r(explode(" ", $my_str, 4));
?>
Related FAQ
Here are some more FAQ related to this topic: