All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Convert String To Array In PHP

Last Updated : Mar 11, 2024

How To Convert String To Array In PHP

In this article we will show you the solution of how to convert string to array in PHP, to convert string to the array we will use two functions - str_split() and explode().

Str_split(): this str_split() function is used to splits strings into arrays.

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 How To Convert String To Array In 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> how to convert string to array in 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> how to convert string to array in php </h3>
</body>
</html>
<?php
$string = "this is a string" ;
echo "<pre>" ;
echo $string ;
echo "<hr />" ;
$arr = str_split ($string) ;
print_r ($arr) ;
echo "<hr />" ;
$arr = str_split ($string,3) ;
print_r ($arr) ;
echo "<hr />" ;
$arr = explode (" ",$string) ;
print_r ($arr) ;
echo "<hr />" ;
$arr = explode ("s",$string) ;
print_r ($arr) ;
echo "<hr />" ;
?>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. <style> tag to add CSS to the HTML page
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Close the HTML tag with </html>
  9. <?php tag to write the php.
  10. At first $string variable to store a string
  11. Using echo to display the string
  12. Creating a variable named $arr with the str_split() function. This will split the string into an array by every character.
  13. Using echo to display the array
  14. Str_split($string, 3) used to split the string by every 3 character
  15. Using echo to display the array
  16. Explode(“ ”, $string) is used to split the string by every word.
  17. Using echo to display the array
  18. Explode(“s”, $string) used to split the string by every s appears.
  19. Using echo to display the array
  20. 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 convert string to an array using PHP.

I hope this article on how to convert string to array in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪