View Full Version : FUNCTION
Romanity
22-12-2007, 06:26 PM
Right let me show you the function... then explain whats wrong =]
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.
Mentor
22-12-2007, 06:34 PM
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)
Romanity
22-12-2007, 06:44 PM
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?
Mentor
22-12-2007, 06:55 PM
Ok, though the ksort and use of the ergi instead of str replace still seems unnessary?
Romanity
22-12-2007, 06:58 PM
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
Mentor
22-12-2007, 09:07 PM
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?
Dentafrice,
22-12-2007, 09:12 PM
I told you the code earlier..
MrCraig
22-12-2007, 09:40 PM
Use entors and add more str_replaces for each thing u wish to replace?
ie
function replace($string)
{
$string = str_replace("{site_domain}", $site_domain, $string);
$string = str_replace("{site_name}",$site_name,$string);
return $string;
}
Mentor
22-12-2007, 10:50 PM
str replace should support arrays just as well as any other function?
Romanity
23-12-2007, 10:39 AM
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 -.-
Dentafrice,
23-12-2007, 01:41 PM
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 -.-
Why do you keep doing this? We have told you how to do it yet you refuse to listen.
MrCraig
23-12-2007, 02:35 PM
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 -.-
Maybe if you listened to what we're trying to tell you it would.
I dont see the point in this thread if your ignoring everything we suggest.
No need to enclose the $string in quotation marks.
Romanity
23-12-2007, 05:50 PM
You actually havent suggested anything :S
Mentor
23-12-2007, 06:15 PM
Well maybe i was giving to much credit in assuming you knew how to correctly use a function as you appear to have ignored almost everything thats been said.. This example "Should" work.
//Create all the vars
$information = "i like to eat {food} while drinking a glass of {drink}";
$myfood = "waffles";
$mydrink = "bleach";
function replace($string)
{
$pattern[0] = "{food}";
$pattern[1] = "{drink}";
$replacement[0] = $myfood;
$replacement[1] = $myfrink;
$string = str_replace($pattern, $replacement, $string);
return $string;
}
$information = replace($information);
echo $information;
}
*correct me if wrong i aint done php in ages
Romanity
23-12-2007, 06:19 PM
Thats the function i had anyway....
Gets the info from the database... runs..
$templateHTML = replace($nav[templateHTML]);
echo "$templateHTML";
But still doesnt change it... i dont think the problem is with the function .. out of the function it works, put it in a function and it breaks????
You get $nav[templateHTML] how? Also you DO NOT need to inclose lone variables in quotation marks fgs. Just slows your script down.
Romanity
24-12-2007, 11:37 AM
I get it with the query...
The query works, because the template is echoed out, but it wont change the variables before it does...
Dentafrice,
24-12-2007, 02:23 PM
fgs
<?php
//Create all the vars
$information = "i like to eat {food} while drinking a glass of {drink} then shopping in {store}";
function replace($string)
{
$myfood = "waffles";
$mydrink = "bleach";
$store = "Wal-Mart";
$find = array("{food}", "{drink}", "{store}");
$replace = array($myfood, $bleach, $store);
$string = str_replace($find, $replace, $string);
return $string;
}
$information = replace($information);
echo $information;
?>
Romanity
24-12-2007, 06:55 PM
Thats what i got...
function replace($string)
{
$pattern = array("{site_domain}", "{site_name}");
$replacement = array("$site_domain", "$site_name");
str_replace($pattern, $replacement, $string);
return $string;
}
All variables are already defined.... but the function wont work... it physically does doesnt change it to anything. Your all saying the same thing, and im saying if you listen, its not working...
Dentafrice,
24-12-2007, 07:24 PM
*Message Removed*
Edited by Agesilaus (Forum Moderator): Please do not be rude.
Baving
01-01-2008, 02:31 PM
Thats what i got...
function replace($string)
{
$pattern = array("{site_domain}", "{site_name}");
$replacement = array("$site_domain", "$site_name");
str_replace($pattern, $replacement, $string);
return $string;
}
All variables are already defined.... but the function wont work... it physically does doesnt change it to anything. Your all saying the same thing, and im saying if you listen, its not working...
If $site_domain and $site_name are already defined in the script then you need to global them into the function:
function replace($string) {
global $site_domain,$site_name;
....
If $site_domain and $site_name are already defined in the script then you need to global them into the function:
function replace($string) {
global $site_domain,$site_name;
....
not always, you could do:
class hi
{
var $sitedomain;
var $site_name;
function replace($string)
{
whatever
}
}
and then do
$function = new hi;
$function->sitedomain = $sitedomain;
$function->site_name = $site_name;
$function->replace("hi");
although yours is easier.
Dentafrice,
01-01-2008, 03:24 PM
If $site_domain and $site_name are already defined in the script then you need to global them into the function:
function replace($string) {
global $site_domain,$site_name;
....
I didn't think you had to global things in unless they were in a class?
Wow, learn something new every day =]
I didn't think you had to global things in unless they were in a class?
Wow, learn something new every day =]
neither did i, and i just learnt what global did lol.
edit: btw, ive only seen global in a class before and didnt have a clue what it meant, so, ye.
Baving
01-01-2008, 03:33 PM
If you do not global outside variables into the function then the newly defined variables within the function will be returned as null.
Example:
$site='32';
function something($string) {
$string = $site.$string;
}
The value of $site within the function would be blank / null.
If you do not global outside variables into the function then the newly defined variables within the function will be returned as null.
Example:
$site='32';
function something($string) {
$string = $site.$string;
}
The value of $site within the function would be blank / null.
so if you did global in a class you would be able to use the variables in all of the class' functions?
Dentafrice,
01-01-2008, 04:05 PM
I believe you need to global it on each function.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.