Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-24-2009, 07:33 AM   PM User | #1
AnniHilatE
New Coder

 
Join Date: Aug 2004
Posts: 24
Thanks: 0
Thanked 3 Times in 3 Posts
AnniHilatE is an unknown quantity at this point
String Formatter with regular expression.

This probably isnt the greatest code to do the job, but it does it none the less.
The only things that need setting up, are the separators and the words you want to retain the formatting for.

PHP Code:
/** AnniHilatE ** 
*  checkString() - A function which will perform
*  pattern replacement ( preg_replace ) leaving 
*  any values contained in ( ' ' ) with their
*  original formatting.
* NOTE:: See below.
**/ 

/**
*    $string = "MY_VARIABLE_1 =   5, MY_VARIABLE_2 =   1, MY_VARIABLE_3 = 2, 
*        MY_VARIABLE_4 =    2, MY_VARIABLE_5 =  'John Doe' , MY_VARIABLE_6  =  3 ";
*    print checkString($string);
*
*    NOTE:: 
*    Currently, any variable which will use ( ' ' ) to avoid losing their spaces
*     MUST be defined as shown in the STR_REPLACE section.
*/

function checkString($text){ 
    
$separator "'"// This is the separator the code will look for
/* textLookup words need to be in the format " MYVAR=' " */
    
$textLookup = array("COUNTRY='","NAME='","AGE='"); //This is the words keeping their formatting.

    
    /*    Create empty variables    */
    
$newText "";
    
$wait false;
    
$endWait false;
    
$newString = array();
    
$yourWords = array();
    
    
/* split the input text into an array */ 
    
$stringArray str_split($text); 

    
/*    go through the array of letters 
    *    and if a $separator is found,
    *    start adding text into the
    *    stored word list ( $yourWords )
    */
    
for($i=0$i count($stringArray); $i++){ 
        if(
$stringArray[$i]!=$separator || $wait){
            if(
$stringArray[$i]==$separator && $wait){
                
$wait false;
                
$endWait true;
            }
            if(
$wait){
                
$t $stringArray[$i];
                
$newText "".$newText."$t";
            }
            if(
$endWait){
                if(
$yourWords[0]!=$newText){
                    
array_push($yourWords$newText);
                    
$newText null;
                    
$endWait false;
                }
            }
        }else{
            
$wait true;
        }
    }
    
/*    Reset the waits    */
    
$wait false;
    
$endWait false;
    
    
/*    Perform regular expression pattern replacement    */
    
$stringArray2 preg_replace('/\s*/m','',$stringArray);
    
    
/*    Put the string into a new array WITHOUT formatted variables    */
    
for($ij=0$ij count($stringArray2); $ij++){ 
        if(
$stringArray[$ij]!=$separator || $wait){
            if(
$stringArray[$ij]==$separator && $wait){
                
$wait false;
            }
            if(!
$wait){
                
$newString[] = $stringArray2[$ij];
            }
        }else{
            
$wait true;
        }
    }

    
/* Re formatting the string */
    /*
    *    NOTE::  textLookup MUST BE IN THE SAME ORDER THEY APPEAR IN YOUR STRING BEING PASSED
    */
    
$formattedString implode(""$newString);
    for(
$z=0;$z<count($textLookup);$z++){
        
/*    Insert formatted variables back INTO the string    */
        
$formattedString str_replace($textLookup[$z], $textLookup[$z].$yourWords[$z]."'"$formattedString);
    }
    
/*    Return the fully formatted string    */
    
return $formattedString;

Input::
PHP Code:
$string "SORT_BUTTON_SPECIAL =   5,COUNTRY='U S A', SORT_ORDER_UP =   1, SORT_ORDER_DOWN = 2, 
        SORT_BUTTON_RANK =    2, NAME =  'John Doe' , ANOTHERVAR  =  3 , AGE  ='2 1'"
;
print 
checkString($string); 
Output::
Code:
SORT_BUTTON_SPECIAL=5,COUNTRY='U S A',SORT_ORDER_UP=1,SORT_ORDER_DOWN=2,SORT_BUTTON_RANK=2,NAME='John Doe',ANOTHERVAR=3,AGE='2 1'
AnniHilatE is offline   Reply With Quote
Reply

Bookmarks

Tags
expression, format, formatting, regular, string

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 05:50 AM.


Advertisement
Log in to turn off these ads.