Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting a string every 4 characters and then put those into new variables?

Tags:

string

php

How would i split a string every 4 characters and then have every 4 characters put into a separate variable so i can do other stuff with each individual 4 characters?

like image 417
RSM Avatar asked Dec 04 '22 08:12

RSM


1 Answers

Well, I don't really see you putting each of them in a different variable, because then you'll have like 10 variables:

$var1 = ...;
$var2 = ...;
$var3 = ...;

But you could use the str_split function as following:

$variable = str_split($origionalvar, 4);

it will create an array, which you can access:

$variable[0] to $variable[sizeof(variable)]

If you don't know about arrays, you really should read up on those. They are really great if you want to store a lot of similar information in an object and you don't want to create a new variable for it every time. Also you can loop over them very easily and do some other great stuff with them.

I assume you probably googled this question a bit too. You must have come across http://php.net/manual/en/function.str-split.php. Did you see this page, and if you did, did you have some trouble with it. If so perhaps we can help you reading the documentation properly.

like image 103
Timo Willemsen Avatar answered Mar 23 '23 01:03

Timo Willemsen