can this code be broken?
- by user105165
Consider the below html string
<p>This is a paragraph tag</p> <font>This is a font tag</font> <div>This is a div tag</div> <span>This is a span tag</span>
This string is processed to tokanize the text found in it and we get 2 results as below
1) Token Array :
$tokenArray == array(
'This is a paragraph tag',
'This is a div tag',
'<font>This is a font tag</font>',
'<span>This is a span tag</span>'
);
2) Tokenized template :
$templateString == "<p>{0}</p>{2}<div>{1}</div>{3}";
If you observe, the sequence of the text strings segments from the original HTML strings is different from the tokenized template
The PHP code below is used to order the tokenized template and accordingly the token array to match the original html string
class CreateTemplates {
public static $tokenArray = array();
public static $tokenArrayNew = array();
function foo($templateString,$tokenArray)
{
CreateTemplates::$tokenArray = $tokenArray;
$ptn = "/{[0-9]*}*/"; // Search Pattern from the template string
$templateString = preg_replace_callback($ptn,array(&$this, 'callbackhandler') ,$templateString); // function call
return $templateString;
}
// Function defination
private static function callbackhandler($matches) {
static $newArr = array();
static $cnt;
$tokenArray = CreateTemplates::$tokenArray;
array_push($newArr, $matches[0]);
CreateTemplates::$tokenArrayNew[count($newArr)] = $tokenArray[substr($matches[0],1,(strlen($matches[0])-2))];
$cnt = count($newArr)-1;
return '{'.$cnt.'}';
} // function ends
} // class ends
Final output is (ordered template and token array)
$tokenArray == array('This is a paragraph tag',
'<font>This is a font tag</font>',
'This is a div tag',
'<span>This is a span tag</span>'
);
$templateString == "<p>{0}</p>{1}<div>{2}</div>{3}";
Which is the expected result.
Now, I am not confident whether this is the right way to achieve this. I want to see how this code can be broken or not.
Under what conditions will this code break? (important)
Is there any other way to achieve this? (less important)