A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

PHP Test Code

1.2k views · started by Chris ·
#1
PHP Test Code
<?php
ob_start();
?>
{if $Lol="Hello"}
true
{if:else}
false
{/if}
<?php
$Content = ob_get_contents();
ob_end_clean();

$Lol = "Hello";

if(preg_match('@^(?:{if )?([^}]+)@i', $Content, $Statement)){
if(preg_match("/=/i", $Statement[1])){
$Data = explode("=", $Statement[1]);

for($i = 0; $i < Count($Data); $i++){
$Data[$i] = trim($Data[$i], " ");

if(isVariable($Data[$i])){
$Data[$i] = ltrim($Data[$i], "$");

if(isset(${$Data[$i]})){
$Data[$i] = ${$Data[$i]};
}
}else{
$Data[$i] = trimString($Data[$i]);
}
}

if(strpos($Content, "{if:") !== false && strpos($Content, "{if:") < strpos($Content, "{/if}")){
if(substr($Content, strpos($Content, "{if:")+4, 4) == "else"){
if($Data[0] == $Data[1]){
echo substr($Content, (strpos($Content, $Statement[1] . "}\r\n") + strlen($Statement[1] . "}\r\n")), (strpos($Content, "{if:else}") - (strpos($Content, $Statement[1] . "}\r\n") + strlen($Statement[1] . "}\r\n"))));
}else{
echo substr($Content, (strpos($Content, "{if:else}\r\n") + strlen("{if:else}\r\n")), (strpos($Content, "{/if}") - (strpos($Content, "{if:else}\r\n") + strlen("{if:else}\r\n"))));
}
}
}else{
if($Data[0] == $Data[1]){
echo substr($Content, (strpos($Content, $Statement[1] . "}\r\n") + strlen($Statement[1] . "}\r\n")), (strpos($Content, "{/if}") - (strpos($Content, $Statement[1] . "}\r\n") + strlen($Statement[1] . "}\r\n"))));
}
}
}
}

function isVariable($String){
if(strpos($String, "$") !== false){
return true;
}

return false;
}

function trimString($String){
$String = trim($String, '"');
$String = trim($String, "'");

return $String;
}
?>[/1][/1][/1][/1][/1][/0][/1][/1][/1][/1][/1][/0][/1][/1]


Lol, well I am sure it is majorly over complicated, just though someone might wanna see it :P
#2
What are you testing with this? It's so hard to follow by just looking at it. XD
#3
Whats this code for O_o
#4
yea what is this code?
#5
Turns text into a if or if/else statement.

So it will parse

{if $Lol="Hello"}
true
{if:else}
false
{/if}


Check if $Lol == "Hello"

and then display the proper data.
#6
I'd stick with using PHP instead of custom code. CodeIgnitor had a small write up on this and found that parsing custom code to PHP causes much lag, when the much faster alternative of straight PHP code isn't any more difficult to understand.
#7
GAMEchief wrote:
I'd stick with using PHP instead of custom code. CodeIgnitor had a small write up on this and found that parsing custom code to PHP causes much lag, when the much faster alternative of straight PHP code isn't any more difficult to understand.


It's an example of how a template system would work.

But it is only setup to parse one if and only that data is shown.

It is my first time working on this type of system and just kinda guessed, not much out there that I could find on the subject.
#8
Would you mind elaborating on the difference between @ boundaries in RegEx and / boundaries?
#9
GAMEchief wrote:
Would you mind elaborating on the difference between @ boundaries in RegEx and / boundaries?


I have no clue the difference I know more about the way humans came to exist then I do about regex.

Lol, I found the preg_match function on php.net and just viewed how other codes got the domains out of url's to make that :P
#10
Ah.
I noticed you switched between them:
if(preg_match('@^(?:{if )?([^}]+)@i', $Content, $Statement)){
if(preg_match("/=/i", $Statement[1])){[/1]


I'd never seen @ used before, tbh. >_>
#11
GAMEchief wrote:
Ah.
I noticed you switched between them:
if(preg_match('@^(?:{if )?([^}]+)@i', $Content, $Statement)){
if(preg_match("/=/i", $Statement[1])){[/1]


I'd never seen @ used before, tbh. >_>


Um if I remember correctly.

If I replaced the @'s with /'s it would five me an error with less/greater than signs.

('{if ' was '<if ')

Other than that I cannot recall anything.
#12
Well then, perhaps like in PHP itself @ means to not give an error. If a symbol is giving you problems, escape it.
You likely could have changed < and > to \< and \> and fixed your problem. If you hadn't tried that.