Thread: Text Parser
View Single Post
  #1  
Old 05-06-2007, 11:26 PM
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
Text Parser

Someone needed this, so I made it.

This reads text, and uses a "Smart Parser" to go through it and change variables to the value. To signal a variable, put @VARNAME@ in the code. GS1 will not work, and things such as servername will also not. I have tested this with clientr, client, player, server, and serverr. They all work. Here are the commands:

PHP Code:
parseFile"path/to/file.txt" );
The way I used this was in a server messageI used Parser.parseFile"data/servermessage.txt" ); This will return an array of the filesimply parsed.

parseTextstring/array )
You can give a string, or an array as a paramaterWill only parse the first param thoughso put things in an array if you want. If you give it an array, it will return an array. If you give it a stringit will return a string
Code for wNPC Parser:
PHP Code:
// Made by Chris Zakuto
function doInit()
{
  
// Must be a single character, sorry!
  
this.sepText "@"// The symbol that seperates variables from text.
}
function 
onCreated()
{
  
doInit();
}

public function 
parseFilefilepath )
{
  if ( 
temp.fileContents != NULL )
  {
    
temp.fileContents NULL;
  }
  
  if ( 
temp.!= NULL )
  {
    
temp.a.clear();
  }
  
temp.fileContents.loadLinesfilepath );
  
  for ( 
temp.ntemp.fileContents )
  {
    
temp.a.addtemp.);
  }
  
  
temp.fileText parseTexttemp.);
  
  return 
temp.fileText;
}
// "Smart Parser" returns the same type (array or string)
public function parseTexttext )
{
  if ( 
temp.returnText != NULL )
  {
    if ( 
temp.returnText.size() > )
    {
      
temp.returnText.clear();
    }
    
    
temp.returnText NULL;
  }
  if ( 
text.size() > )
  {
    for ( 
temp.atext )
    {
      
temp.returnText.addparseLinetemp.) );
    }
  }
  else
  {
    
temp.returnText parseLinetext );
  }
  
  return 
temp.returnText;
}

// Actually parse the text.
function parseLinerealText )
{
    if ( 
temp.chars != NULL )
  {
    
temp.chars.clear();
  }
  
  for ( 
temp.0temp.realText.length(); temp.++ )
  {
    
temp.chars.addrealText.substringtemp.i) );
  }
  
  if ( 
temp.isVar != false )
  {
    
temp.isVar false;
  }
  for ( 
temp.ntemp.chars )
  {
    if ( 
temp.isVar == false // Variable has not been detected.
    
{
      if ( 
temp.== this.sepText // Start of a variable.
      
{
        
temp.isVar true;
      }
      else
      {
        
temp.parsedText temp.parsedText temp.n;
      }
    }
    else
    {
      if ( 
temp.!= this.sepText )
      {
        
temp.varAdd temp.varAdd temp.n;
      }
      else
      {
        
temp.isVar false;
        
temp.finalVar makevartemp.varAdd );
        
temp.parsedText temp.parsedText temp.finalVar;
        
temp.varAdd "";
      }
    }
  }
  
  
// temp.parsedText is final output.
  
return temp.parsedText;

To use this, input text like this:
PHP Code:
Welcome to @serverr.servername@, @player.account@:@player.nickof @player.guild@! You currently have @clientr.moneymoney, and you have been online for @player.onlinetimeseconds! If we had a player.ip variableyours would be @player.ip@. 
Have fun, please leave comments.
__________________
Reply With Quote