Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-31-2008, 10:59 PM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
explode() & implode()

Some PHP classics.

explode(delimiter, string, limit); - returns an array from a string, split by the given delimiter. Optionally, a limit can be supplied.

- delimiter: a boundary string
- string: the source string
- limit: if limit is set, the returned array will contain a maximum number of limit elements, with remaining part of string attached to the last element. If limit is negative, all but the last limit elements are returned.
Note that if delimiter is set to an empty string ("") or 0, explode() will return FALSE.


PHP Code:
function explode(delimiterstringlimit) {
  
temp.tokens temp.string.tokenize(temp.delimiter);
  if(!
temp.delimiter || temp.delimiter == "") {
    return 
false;
  }
  if(!
temp.limit || temp.limit == 0) {
    return 
temp.string.tokenize(temp.delimiter);
  }
  if(
temp.limit temp.tokens.size()) {
    
temp.limit temp.tokens.size();
  }
  else if(
temp.limit 0) {
    for(
temp.0temp.<= temp.limit 1temp.++) {
      
temp.output.add(temp.tokens[temp.i]);
    }
    for(
temp.0temp.temp.tokens.size() - temp.limittemp.++) {
      
temp.output[temp.limit 1] @= temp.delimiter temp.tokens[temp.limit temp.i];
    }
  }
  else {
    
temp.limit *= -1;
    for(
temp.0temp.<= temp.limit 1temp.++) {
      
temp.output.add(temp.tokens[temp.i]);
    }
  }
  return 
temp.output;

Example:
PHP Code:
function onCreated() {
  
temp.string "this!is!my!string";
  
temp.explode("!"temp.string);
  
temp.i2 explode("!"temp.string2);
  
temp.i3 explode("!"temp.string, -2);
  echo(
"Starting string: " temp.string);
  echo(
"Exploded: " temp."; " temp.i2 "; " temp.i3);

Returns:
Quote:
Starting string: this!is!my!string
Exploded: this,is,my,string; this,is!my!string; this,is
------------------------------------------------------------------------

implode(glue, pieces); - joins array elements with an optional glue string.

- glue: the separating string
- pieces: an array
PHP Code:
function implode(gluepieces) {
  for(
temp.0temp.temp.pieces.size(); temp.++) {
    if(
temp.glue) {
      if(
temp.!= temp.pieces.size() - 1) {
        
temp.output @= temp.pieces[temp.i] @ temp.glue;
      }
      else {
        
temp.output @= temp.pieces[temp.i];
      }
    }
    else {
      
temp.output @= temp.pieces[temp.i];
    }
  }
  return 
temp.output;

Example:
PHP Code:
function onCreated() {
  
temp.pieces = {
    
"this""is""my""array"
  
};
  
temp.implode(nulltemp.pieces);
  
temp.i2 implode(","temp.pieces);
  
temp.i3 implode("#"temp.pieces);
  
temp.i4 implode("-+-"temp.pieces);
  echo(
"Starting array: " temp.pieces);
  echo(
"Imploded: " temp."; " temp.i2 "; " temp.i3 "; " temp.i4);

Returns:
Quote:
Starting array: this,is,my,array
Imploded: thisismyarray; this,is,my,array; this#is#my#array; this-+-is-+-my-+-array
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
  #2  
Old 01-01-2009, 02:00 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Very nice.
__________________
Reply With Quote
  #3  
Old 01-01-2009, 02:38 PM
Codein Codein is offline
jwd
Codein's Avatar
Join Date: Oct 2005
Location: Greater Manchester
Posts: 2,423
Codein has a spectacular aura aboutCodein has a spectacular aura about
Send a message via AIM to Codein Send a message via MSN to Codein
Nice work
Reply With Quote
  #4  
Old 01-09-2010, 06:46 PM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
I have an update... a year later. There is something wrong with the explode() function currently in the first post. I can't remember exactly what it is, but the output is incorrect for certain situations. The following version should fix those problems:

PHP Code:
public function explode(delimiterstringlimit) {
  
temp.tokens temp.string.tokenize(temp.delimiter);
  if(!
temp.delimiter || temp.delimiter == "") {
    return 
false;
  }
  if(!
temp.limit || temp.limit == 0) {
    return 
temp.string.tokenize(temp.delimiter);
  }
  if(
temp.limit temp.tokens.size()) {
    
temp.limit temp.tokens.size();
  }
  
  if(
temp.limit 0) {
    for(
temp.0temp.<= temp.limit 1temp.++) {
      
temp.output.add(temp.tokens[temp.i]);
    }
    for(
temp.0temp.temp.tokens.size() - temp.limittemp.++) {
      
temp.output[temp.limit 1] @= temp.delimiter temp.tokens[temp.limit temp.i];
    }
  }
  else {
    
temp.limit *= -1;
    for(
temp.0temp.<= temp.limit 1temp.++) {
      
temp.output.add(temp.tokens[temp.i]);
    }
  }
  return 
temp.output;

__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 02:18 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.