...

View Full Version : String Formatter with regular expression.



AnniHilatE
04-24-2009, 07:33 AM
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.



/** 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::


$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::

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'



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum