Jump to content

Recommended Posts

I have the following that i am stil working on but wish to know if this will work when finished.

 

// split up the string in to an array.
  function split_to_array($string) {
  // can I do this?  will the array be usable from outside this function?
  $newarray = preg_split("/\|\|/",$string); // split each section up where the || divider is.
    return $newarray;
  }

You have to assign the function call to a variable so that the returned $newarray gets passed back:

 

$array = split_to_array($string);

 

Then $array will have the value of $newarray value.

 

Alternatively, you can do like

 

global $newarray;

 

at the beginning, inside your function.  This will make $newarray globally scoped.  Then you won't have to return anything.  However, I do not recommend doing this.  Go for the first suggestion: assign the returned value to a variable.

 

p.s.- as nightslyr mentioned, explode() is better.  It is optimized for splitting strings by another static string.  You should only use preg_split if you are trying to split by a non-static pattern.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.