I am having severe issues with this, and I am hoping that I won't have to write a str_replace function for this.

Essentially I want to import a string from an XML file, and echo the string. The catch: the string contains a valid variable name, and echo does not translate that variable name into the variable value like I want it to.

A string imported from the xml file would look like this:

<a href="$attribs['HREF']" target="$attribs['TARGET']"

Note the

$attribs[' ']

part which is a valid array name in general and within my function.

Also note that this string is contained within an array, so that the echo command would simply be

echo $varname[$index]

Any ideas? I would prefer not to have to use a replace function for sure simplicity. The whole idea is that I'm not going to have any clue what the variable name might be prior to accessing the XML file.

THANKS FOR ANY INPUT!!

Josh

    The best way would be to just use a replace function. If you do use a replace function, don't use a variable name, use something else like this:

    <a href="$attribs['HREF']" target="$attribs['TARGET']"

    change it to

    <a href="{HREF}" target="{TARGET}"

    I hate to post this even though you don't want to use str_replace, but it's much easier.

      Thanks Cheerio for your help - I am definately going to have to consider this as a possible solution.

      I'm surprised PHP in general lacks such a feature. Like I posted earlier, I want to refrain from using a replace statement simply because it sincerely complicates the method that my code uses.

      I have tried eval() statements, renaming, etc. to no avail.

      I think that echo is treating the contents of the variable as an equivalent to a single quote ( ' ) encapsed string as opposed to a double quote one ( " ). Is there any way that anybody knows to force echo, either through string manipulation or otherwise to treat the variable's contents as if it were a static, hardcoded string?

      Thanks again,

      Josh

        Just a quick update. If I do go with a string replace loop, what method would be the most efficient considering the following: a) I do not know the variable name in question b) nor can/will it be a fixed length.

        I do hope PHP has some functions built in that will allow me to replace the key terms without having to worry about lengths and looping through until you find the terminating character. I was thinking eregi_replace, but that seems a little complicated, atleast while I'm as tired as I am.

        Any ideas would be appreciated,

        Thanks!

          eval("echo \"$string\";");
          

          Where $string contains that entire string you want to output.

            <sigh> Why do I read posts when I am tired.

              NogDog, thanks but it still does not work. Now, it's returning unexpected T_STRING errors. Nevertheless, the statement you suggested will not replace the variable names within the variable with their contents - but merely echo the variable's names instead.

              To be a little more forthcoming on my code here is a rough idea of what I am doing:

              $attributes['HREF']= 'An HREF Imported by the parser as an attribute';
              
              $xml_in[$key]='Some key that the XML parser brought in including this variable name $attributes['HREF']';
              
              echo $xml_in[$key];

              Returns:

              Some key that the XML parser brought in including this variable name $attributes['HREF']

              I want it to return:

              Some key that the XML parser brought in including this variable name An HREF Imported by the parser as an attribute

                Why not export the string after it parses?
                If you have a code that parses XML you can run it through the XML parser than export the string.

                  Harmor: there is no easy way to export the string from the parser because the initial XML file provides template data for a secondary XML file. The initial XML file contains template code which is echoed out after parsing a secondary XML file containing the actual page data and what markups to use.

                  The template XML file would contain HTML code blocks etc. which have specific variables that need to be defined such as <a href=""> etc. The actual link information would come from the secondary XML file. in the form of <link href="test.htm">THIS IS A TEST</link>. The final data that would end up in the browser screen is <a href="test.htm" target="_blank" onMouseOver etc. etc.>THIS IS A TEST</a>

                    So I caved in, and decided to use str_replace to replace all entities in the function. Here is the block of code:

                    $outdata = $structure[($name.'START')];
                    foreach($attribs as $key=>$value)
                    {
                    	$outdata = str_replace('{'.$key.'}',$value,$outdata);
                    }
                    echo $outdata;

                    It takes the template data to be reformatted, then checking all of the attributes passed to the function, searches the template string for the appropriate tag to replace and replaces it. Then, it outputs the reformatted code.

                    Normally, I shy away from loops and repeating constructs simply because there is often a much simpler way. In this case the foreach loop was the most simple.

                    Thanks guys for your help, and if anyone does come up with a good one liner, I might just use it.

                    Thanks!!

                    Josh

                      Write a Reply...