php

How to convert a string into an array in PHP

In this tutorial, we are going to see how to convert a string into an array in PHP. You can use the explode() function in PHP to convert or split a string into an array by a separator such as space, comma, etc. You can also set the optional limit parameter to specify the number of array elements to return.
 

Convert a string into an array
<?php
	$str = 'Welcome to StackHowTo.';
	print_r(explode(" ", $str));
?>

Output:

Array
(
    [0] => Welcome
    [1] => to
    [2] => StackHowTo.
)
[st_adsense]  

Convert a string into an array with limit parameter
<?php
	$str = 'Welcome to StackHowTo.';
	print_r(explode(" ", $str, 2));
?>

Output:

Array
(
    [0] => Welcome
    [1] => to StackHowTo.
)
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More [st_adsense]

Leave a Reply

Your email address will not be published. Required fields are marked *