Jump to content

Convert String to Array... no luck

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
ProServ

ProServ

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi guys,

This seems really simple to me, but I've had no luck. I need help! :)

I've got file upload script which is working just fine. Here's the code I'm using to define the file extensions which are uploadable.

$uploadtypes = array( "png", "gif", "jpeg", "jpe", "jpg", "fla", "html", "doc"); 

So, here's my problem. I want this editable in the GUI. So, I added a field in my MySQL database and had $uploadtypes equal the field.

I've tried several functions to separate the values and to explode into an array. No luck.

I want the end-use to type
"png, gif, jpeg, fla" etc into a string. Then, I need that coverted into the PHP line above.

Any suggestions?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
If I understood right:
$stringtypes = "png, gif, jpeg, fla";
$uploadtypes = explode(", ", $stringtypes);


#3
adserverexpert

adserverexpert

    Newbie

  • Members
  • PipPip
  • 21 posts
is the above code is enough for you to edit the types ?

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

adserverexpert said:

is the above code is enough for you to edit the types ?

What do you mean? PHP is very loosely typed so generally type is not a concern. For example:

$stringtypes = "1, 2, 3";

$uploadtypes = explode(", ", $stringtypes);

echo $uploadtypes[0] + 5;

since $stringtypes is a string, $uploadtype[0] is naturally a string. Nonetheless, you can perform arithmetic operations on it. The above example will output 6.

#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Based on the code above you already have an array so why would you want to explode it? You may want to look at implode.

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I believe the array he posted is the end result he is looking for.

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
The two lines before the code:

Quote

I've got file upload script which is working just fine. Here's the code I'm using to define the file extensions which are uploadable.

I'm not so sure. Perhaps he should explain?

#8
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
However, the last two lines he says this:

Quote

I want the end-use to type
"png, gif, jpeg, fla" etc into a string. Then, I need that [the string] coverted into the PHP line above [the array].


#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
So, he has an array (according to the first two lines) but wants to convert a string to the same array he has (according to the last two lines).

@ProServer: What do you want? Can you clarify?

I have an answer for the meantime:

$myArray = split(join(explode(implode($myStringArray, ","), ","),",") , ",");

Edited by Jordan, 02 October 2008 - 06:35 AM.


#10
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
LOL nice Jordan.