How to create an array from PHP string? Explain with example?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

we can create an array from php string with the help of str_split( ) function in PHP, str_split() is an inbuilt function in PHP and is used to convert the given string into an array. this function basically splits the given string into smaller strings of length specified by the user and stores them in an array and returns the array.

stntax

array str_split($orginal_string, $splitting_length)

This function take two parameters

  • $original_string=  the original string that the user needs to split into an array.
  • $splitting_length=  the length of each array element

example

<?php

  //without using length
$string = "SHIVAM";
  
   echo "<pre>";
print_r(str_split($string));
    echo "<pre>";

           /* OR */
  
//with length
$string = "COTOCUSFAMILY";

  echo "<pre>";
print_r(str_split($string, 3));
   echo "<pre>";

?>

output

Array
(
    [0] => S
    [1] => H
    [2] => I
    [3] => V
    [4] => A
    [5] => M
)
Array
(
    [0] => COT
    [1] => OCU
    [2] => SFA
    [3] => MIL
    [4] => Y
)

Related Posts

Modernizing Software Delivery in the Dutch Tech Landscape: A Pragmatic DevOps Playbook

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Finding Qualified AWS DevOps Trainers: Enterprise Playbook

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Teaching Failure: How to Build Incident Response and Resilience into DevOps Training

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

The Open Road in Japan: An Independent Rider’s Handbook for Cross-Country Touring

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

A Comprehensive Guide to Scooters, Motorcycles, and Road Trips

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Monitoring and Observability Training: Complete Curriculum Guide

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x