views:

143

answers:

4

Hi.

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"

+2  A: 

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++])));
}
Col. Shrapnel
+1: for mentioning "hard case" :)
Simon
Probably my regex is bad, because it haven't cut off large part of site.
Misiur
+2  A: 

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
Simon
+2  A: 

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.

svens
+1  A: 

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.

nickf