Be a Supporter!

php question

  • 394 Views
  • 14 Replies
New Topic Respond to this Topic
JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
php question 2006-07-22 19:56:44 Reply

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
Response to php question 2006-07-22 19:58:06 Reply

explode?


BBS Signature
JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to php question 2006-07-22 20:00:16 Reply

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
Response to php question 2006-07-22 20:05:03 Reply

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.

BBS Signature
DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to php question 2006-07-22 20:11:43 Reply

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.


BBS Signature
JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to php question 2006-07-22 20:19:53 Reply

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
Response to php question 2006-07-22 20:20:34 Reply

Yeah, I just noticed that. Glad it works now.


BBS Signature
JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to php question 2006-07-22 20:21:48 Reply

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.
Forum Stats
Member
Level 37
Musician
Response to php question 2006-07-23 00:21:04 Reply

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
Response to php question 2006-07-23 00:25:00 Reply

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.


BBS Signature
Tigiot
Tigiot
  • Member since: Jul. 22, 2006
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to php question 2006-07-23 00:26:10 Reply

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
Response to php question 2006-07-23 00:56:10 Reply

isn't there a push function in PHP?

yep:
http://us3.php.net/m..ction.array-push.php

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to php question 2006-07-23 00:58:22 Reply

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.


BBS Signature
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to php question 2006-07-23 01:57:48 Reply

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
Response to php question 2006-07-23 02:01:50 Reply

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.


BBS Signature