Jump to content

[SOLVED] Turning a string into an array


flappy_warbucks

Recommended Posts

Hi,

 

Is there any way to turn a string into an array (well obviously there is a way to do it, i just cant figure out how)

 

i was thinking there must be a class in PHP what allows you to do that, either in a loop, or something built in. i.e.

 

class_name("4", $mystring);

 

its buggering me, so pwease help

 

*puppy dog eyes*

 

thankys :)

Link to comment
https://forums.phpfreaks.com/topic/42499-solved-turning-a-string-into-an-array/
Share on other sites


<?php
$var1="abcdefghij";
for($i=0;$i<strlen($var1);$i++)
     echo $var1[$i].'<br>';
?>

 

 

Using that code above... How would i select more than one letter at a time and put it into individual strings for example:

 

If i used that code and wanted to be able to so this:

 

echo $var1[abc]; ( outputs 'abc' )

echo $var1[def]; ( outputs 'def' )

echo $var1[ghij]; ( outputs 'ghij' )

 

 

How would i modify that script to do that?

 


<?php
$var1="abcdefghij";
for($i=0;$i<strlen($var1);$i++)
     echo $var1[$i].'<br>';
?>

 

 

Using that code above... How would i select more than one letter at a time and put it into individual strings for example:

 

If i used that code and wanted to be able to so this:

 

echo $var1[abc]; ( outputs 'abc' )

echo $var1[def]; ( outputs 'def' )

echo $var1[ghij]; ( outputs 'ghij' )

 

 

How would i modify that script to do that?

 

 

what that code does is effectively give you an array from the string (which is what i wanted to do), if you wanted individual strings then i would reference that part of the array instead of making a batch of new strings.

 

why make more work for yourself?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.