Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts

    $Strings in an array

    Hi All

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

    Code:
    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:
    <&pb(46,6.001,b,0,K,100,"Solid",n,100,1,m,100.877,100.877,0,1.578,0,0,"c:/artwork/Divider_01_xtags",,"")>

    Cheers
    Steve

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    81
    Thanks
    0
    Thanked 11 Times in 11 Posts
    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.

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi

    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!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •