-
FUNCTION
Right let me show you the function... then explain whats wrong =]
PHP Code:
function replace($string)
{
$pattern[0] = "{site_domain}";
$replacement[0] = "$site_domain";
ksort($pattern);
ksort($replacement);
eregi_replace("$pattern", "$replacement", "$string");
return $string;
}
It is designed such that, when I add other values, it will do the same for them... Right, in my database is {site_domain} and im trying to replace it to be $site_domain (a previously defined variable).
However, with and/or without the function, the end result for {site_domain} is %7bsite_domain%7d yet no where in the coding is it told to htmlspecialchars itself. And when trying to use the function but replace %7b to $, it still doesnt work -.-
Anyone got a function that will change the {x} to $x for any variable... that works =]
Moved by Agesilaus (Forum Moderator) from Design and Development: Please post in the correct forum.
-
Simple answer, there the {} ?
Secondly why the hell are you useing arrays anyway when you only have a single value?? or performing a direct string replace with the eregi command even? its kinda a waste or proccessing.
A correct and simpler version of your code could be done with a simple
function replace($string)
{
$string = str_replace("{site_domain}", $site_domain, $string);
return $string;
}
tho its kinda pointless as theres no reason for it to be a function if it is really only doing one task "/ (you may as well just use the str replace function itself in there)
-
Like i said.. it have more variables...
Ill be adding them as i go along, so indeed, the function is needed, its easier for when it comes to the CMS system.
Any idea on how to make it work?
-
Ok, though the ksort and use of the ergi instead of str replace still seems unnessary?
-
Oki, changed to preg_replace... however the function still wont work...
Any idea how to get it to work.. i think the problem is that its being htmlspecialchars without being told to... :S
-
No, its becuse your useing preg_replace instead of string... preg uses regex style patterns, which likely mans {} are read as parmiters for the replace string and in tern not replaced in the final document. why is it your so opposed to useing str_replace when replaceing strings?
-
I told you the code earlier..
-
Use entors and add more str_replaces for each thing u wish to replace?
ie
PHP Code:
function replace($string)
{
$string = str_replace("{site_domain}", $site_domain, $string);
$string = str_replace("{site_name}",$site_name,$string);
return $string;
}
-
str replace should support arrays just as well as any other function?
-
PHP Code:
function replace($string)
{
$pattern[0] = "{site_domain}";
$pattern[1] = "{site_name}";
$replacement[0] = "$site_domain";
$replacement[1] = "$site_name";
ksort($pattern);
ksort($replacement);
str_replace($pattern, $replacement, "$string");
return $string;
}
Still wont replace the {site_domain} with its value -.-