Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   replacestring (https://forums.graalonline.com/forums/showthread.php?t=78398)

Novo 01-19-2008 05:42 AM

replacestring
 
Uses my TMap 'class'... Thought it might be useful for generic converters.
( Note: This can't convert a Muslim to being Christian. )

PHP Code:

function onCreated()
{
  
this.codeMap = TMap.newInstance();
  
this.codeMap.put( "&", "&" );
  
this.codeMap.put( "<", "&ltag;" );
  
this.codeMap.put( ">", "&rtag;" );
}

function 
encode( oldString )
{
  return 
convert( oldString, this.codeMap );
}

function 
decode( oldString )
{
  return 
convert( oldString, TMap.inverse( this.codeMap ) );
}

function 
convert( oldString, temp.codeMap )
{
  
temp.newString = oldString;
  
temp.codes = temp.codeMap.keySet();
  for ( 
temp.i = 0; temp.i < temp.newString.length(); temp.i ++ )
  {
    for ( 
temp.code: temp.codes )
    {
      if ( 
temp.newString.substring( temp.i, temp.code.length() ) == temp.code )
      {
        
temp.replace = temp.codeMap.get( temp.code );

        
temp.newString =
          
temp.newString.substring( 0, temp.i )
            @ 
temp.replace @
          
temp.newString.substring( temp.i + temp.code.length() );
        
        
temp.i += temp.replace.length() - temp.code.length();
      }
    }
  }
  
  return 
temp.newString;
} 


bscharff 01-19-2008 06:02 AM

Cool!


All times are GMT +2. The time now is 11:48 AM.

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