View Full Version : $Strings in an array

12-04-2008, 11:48 AM
Hi All

Wonder if you can help. I have the following replacement function:

function divider_insert($text) {

$dividers = array (
"ooOoo" => '<&pb(46,6.001,b,0,K,100,"Solid",n,100,1,m,100.877,100.877,0,1.578,0,0,"'.$BMDs_Artwork_Location.'Divider_01_xtags",,"")>'

foreach ($dividers as $k => $v)
$text = preg_replace("/\b$k\b/i",$v,$text);
return $text;

Now the string $BMDs_Artwork_Location is located in a master settings file. However the above simply replaces the text ooOoo with <&pb(46,6.001,b,0,K,100,"Solid",n,100,1,m,100.877,100.877,0,1.578,0,0,"Divider_01_xtags",,"")>
and does not insert the value of the string ($BMDs_Artwork_Location) into the array. Is it possible to do what I'm after?

The value of the string ($BMDs_Artwork_Location) is simply "c:/artwork/", what essentially is a path to the file Divider_01_xtags but want to set the path in my master settings.

Therefore the above should replace ooOoo with:


12-04-2008, 04:28 PM
It kind of looks like that variable has no value, because I agree with you, it should be included in the replace. Try assigning that entire string to a work variable, echo it out, and make sure it contains that path.

12-04-2008, 05:35 PM
You are in a function, so the variable $BMDs_Artwork_Location may not be in the right scope. So you would probably need to pass it to the function for it to be able to reference it.

12-04-2008, 06:35 PM

Thanks for the help. Just did an include with in the function to the settings file and it works - I'd assumed that as the function was on the same page as the already included file it would pick it up.

It would make more sense to pass the string now you mention it but that would mean correcting a lot more pages! So the 'dirty' include will have to do for now.

Thanks again!

12-04-2008, 08:06 PM
Anytime you create a function it is sandboxed, the only variables it can access are superglobals, so $_GET, $_POST, $_REQUEST, $_SESSION, $_COOKIE, $_FILES, $_SERVER, $_ENV and $GLOBALS, and any variables declared within it. Any other variables need to either be passed or globalized within the function call. Where the function is defined is irrelevant, it still can't see it even if its declared on the line above the function signature.

Its bad practice, but if you have a ton of these functions (which would defeat the purpose of it being in a function), you should be able to add the variable to you're $GLOBALS array to allow immediate access. That is a temporary solution though, the functions should be modified to access this variable either through passing or global keyword (global is something that you should use if you're more of an intermediate -> expert programmer, since it can lead to... problems).