php question
- JeremysFilms
-
JeremysFilms
- Member since: Feb. 18, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
how could I take a string and push each character into an array?
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
- JeremysFilms
-
JeremysFilms
- Member since: Feb. 18, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
At 7/22/06 07:58 PM, GamesCool wrote: explode?
But you need to use a separator for that, so how could I just take every single character and push it into an array.
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
Ok buddy, JUST as easy lol.
function make_string_array($string)
{
$mychrs = array();
$strlen = strlen($string);
for ($i = 0; $i < $strlen; $i++)
{
$mychrs[$i] = substr($string, $i, 1);
}
return $mychars;
}
$somechrs = make_string_array('hello');
THERE.
I didn't test it so you might get some errors.
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
Augh I can't fregin test it because the filemanager in Plesk decided not to work. How convenient.
I wrote it quickly, so again, let me know.
- JeremysFilms
-
JeremysFilms
- Member since: Feb. 18, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
Works, except that sometimes you used $mychars and sometimes you used $mychrs. But here's the fixed version:
function make_string_array($string)
{
$mychars = array();
$strlen = strlen($string);
for ($i = 0; $i < $strlen; $i++)
{
$mychars[$i] = substr($string, $i, 1);
}
return $mychars;
}
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
- JeremysFilms
-
JeremysFilms
- Member since: Feb. 18, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
At 7/22/06 08:20 PM, GamesCool wrote: Yeah, I just noticed that. Glad it works now.
Thanks much :D
- Pilot-Doofy
-
Pilot-Doofy
- Member since: Sep. 13, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (12,142)
- Block
-
- Forum Stats
- Member
- Level 37
- Musician
You can just use explode() and use a blank separator and it will split the string into characters and store them in the returned array.
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 7/23/06 12:21 AM, Pilot-Doofy wrote: You can just use explode() and use a blank separator and it will split the string into characters and store them in the returned array.
That's exactly what I thought at first :) Then I looked at the documentation and it said you can't have a blank separator (nor does it work when you test it). That's why I wrote that function.
- Tigiot
-
Tigiot
- Member since: Jul. 22, 2006
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
At 7/23/06 12:21 AM, Pilot-Doofy wrote: You can just use explode() and use a blank separator and it will split the string into characters and store them in the returned array.
Was thinking the same exact thing, also another method but I forget the exact function.
- THESWIRLINGVERTEZIAM
-
THESWIRLINGVERTEZIAM
- Member since: Jul. 8, 2006
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
isn't there a push function in PHP?
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 7/23/06 12:56 AM, thisisobviouslyanalt wrote: isn't there a push function in PHP?
yep:
http://us3.php.net/m..ction.array-push.php
That doesn't really have anything to do with actually taking the characters from a predefined string.
- Pilot-Doofy
-
Pilot-Doofy
- Member since: Sep. 13, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (12,142)
- Block
-
- Forum Stats
- Member
- Level 37
- Musician
At 7/23/06 12:25 AM, GamesCool wrote: That's exactly what I thought at first :) Then I looked at the documentation and it said you can't have a blank separator (nor does it work when you test it). That's why I wrote that function.
I swear to all that is holy that that method used to work. Maybe they modified it in PHP5? I don't know, but I could have sworn I had used it before. Anyway, PHP stores strings as optional NULL-terminated arrays anyway, so you can access the characters like that.
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 7/23/06 01:57 AM, Pilot-Doofy wrote: I swear to all that is holy that that method used to work. Maybe they modified it in PHP5? I don't know, but I could have sworn I had used it before. Anyway, PHP stores strings as optional NULL-terminated arrays anyway, so you can access the characters like that.
Ha, that's weird. I used to be running PHP 4 on my server but I'm running PHP 5 now so I can't test it. It very well could have been changed.
That's very interesting with the string array, as that's a nice easy way of doing it. I didn't know about that method.


