0

I'm doing some kind of own templates system. I want to change

<title>{site('title')}</title>

Into function "site" execution with parameter "title". Here's

private function replaceFunc($subject)
{
    foreach($this->func as $t)
    {
        $args = explode(", ", preg_replace('/\{'.$t.'\(\'([a-zA-Z,]+)\'\)\}/', '$1', $subject));
        $subject = preg_replace('/\{'.$t.'\([a-zA-Z,\']+\)\}/', call_user_func_array($t, $args), $subject);
    }
    return $subject;
}

Here's site:

function site($what)
{
    global $db;
    $s = $db->askSingle("SELECT * FROM ".DB_PREFIX."config");

    switch($what)
    {
    case 'title':
        return 'Title of page';
        break;
    case 'version':
        return $s->version;
        break;
    case 'themeDir':
        return 'lolmao';
        break;
    default:
        return false;
    } 
}

I've tried to compare $what (which is for this case "title") with "title". MD5 are different. strcmp gives -1, "==", and "===" return false. What is wrong? ($what type is string. You can't change call_user_func_array into call_user_func, because later I'll be using multiple arguments)

Edit:

Strlen $what - strlen title 403 - 5 Heh - looks like I haven't cut the rest ;)

var_dump

string(403) " title"

1
  • BTW, this is how one writes software with security holes.
    – Ingo
    May 23, 2010 at 16:14

4 Answers 4

2

MD5 are diffrent. Strcmp gives -1, "==", and "===" return false.

Throw in var_dump() and strlen()

And this function for especially hard cases:

function dump(&$str) {
  $i=0;
  while (isset($str[$i])) echo strtoupper(dechex(ord($str[$i++])));
}
1
  • Probably my regex is bad, because it haven't cut off large part of site.
    – Misiur
    May 23, 2010 at 15:29
2

Have you tried to trim the whitespaces?

$what = trim($what)

Maybe there is a trailing/beginning whitespace character. Also make sure they are both equally cased:

$what = strtolower(trim($what)) //trim and lower
2

Are you sure that there aren't any whitespaces? Use trim() to get rid of them. If the md5s are different the strings are different. var_dump(str_split($what)) will output the string char by char, if a whitespace isn't your problem maybe this helps.

1

I've tried to compare $what (which is for this case "title") with "title". MD5 are different.

That would suggest that $what is not "title". You should put in some debugging statements in there:

function site($what) {
    var_dump($what);
    die();
}

Check there's no extra spaces or characters you weren't expecting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.