Variable data mystery

Ok, this is the oddest thing i have encountered.

I am working on a CMS that provides a template token which renders out some html. I want to obtain that html into a variable and have my way with it. So, im thinking ob_start etc.

Here’s the situation:

The template token: [special_section:edition_index]

This renders:

<a href="/special_section/4/">Answer Book</a><br/><br/>
<a href="/special_section/9/">Home Experts</a><br/><br/>
<a href="/special_section/11/">Young at Heart</a><br/><br/>
<a href="/special_section/28/">Bridal</a><br/><br/>
<a href="/special_section/29/">Complete Wellness</a><br/><br/>
<a href="/special_section/31/">Parade of Homes</a><br/><br/>
<a href="/special_section/37/">Summer Camp</a><br/><br/>
<a href="/special_section/38/">Recreation Services</a><br/><br/>

So to grab this into a variable:

ob_start(); ?>
[special_section:edition_index]
<?php 
$data=ob_get_contents();
ob_end_clean();
?>

Then I echo out the $data variable:

echo 'DATA: ';
echo $data;

Which produces exactly what I want:

DATA: <a href="/special_section/4/">Answer Book</a><br/><br/>
<a href="/special_section/9/">Home Experts</a><br/><br/>
<a href="/special_section/11/">Young at Heart</a><br/><br/>
<a href="/special_section/28/">Bridal</a><br/><br/>
<a href="/special_section/29/">Complete Wellness</a><br/><br/>
<a href="/special_section/31/">Parade of Homes</a><br/><br/>
<a href="/special_section/37/">Summer Camp</a><br/><br/>
<a href="/special_section/38/">Recreation Services</a><br/><br/>

So now, I want to run this $data variable through preg_match_all() to get some particular data. First, I just grab any old character just to get going:

preg_match_all('/./', $data, $matches);
print_r($matches);

Then to my surprise, I get this:

[0] => Array
        (
            [0] => 	
            [1] => 	
            [2] => [
            [3] => s
            [4] => p
            [5] => e
            [6] => c
            [7] => i
            [8] => a
            [9] => l
            [10] => _
            [11] => s
            [12] => e
            [13] => c
            [14] => t
            [15] => i
            [16] => o
            [17] => n
            [18] => :
            [19] => e
            [20] => d
            [21] => i
            [22] => t
            [23] => i
            [24] => o
            [25] => n
            [26] => _
            [27] => i
            [28] => n
            [29] => d
            [30] => e
            [31] => x
            [32] => ]
            [33] => 	
            [34] => 	
        )

)

How in the heck is my $data var showing me one thing when I echo it out, then as soon as I add it to preg_match_all() it reverts to something else??? :sick:

Any thoughts anyone?

Thanks.

At the time you call preg_match_all, $data has a different value. You either changed it, or it is a different variable. If you echo $data on the line immediately before calling preg_match_all, you will see this.

That is what I did. Here is my script in full:


<?php  ob_start(); ?>
[special_section:edition_index]
<?php 
$data=ob_get_contents();
ob_end_clean();

echo 'DATA: ';
echo $data;

preg_match_all('/./', $data, $matches);
print_r($matches);

Which then outputs:


DATA: <a href="/special_section/4/">Answer Book</a><br/><br/>
<a href="/special_section/9/">Home Experts</a><br/><br/>
<a href="/special_section/11/">Young at Heart</a><br/><br/>
<a href="/special_section/28/">Bridal</a><br/><br/>
<a href="/special_section/29/">Complete Wellness</a><br/><br/>
<a href="/special_section/31/">Parade of Homes</a><br/><br/>
<a href="/special_section/37/">Summer Camp</a><br/><br/>
<a href="/special_section/38/">Recreation Services</a><br/><br/>

Array
(
    [0] => Array
        (
            [0] => 	
            [1] => 	
            [2] => [
            [3] => s
            [4] => p
            [5] => e
            [6] => c
            [7] => i
            [8] => a
            [9] => l
            [10] => _
            [11] => s
            [12] => e
            [13] => c
            [14] => t
            [15] => i
            [16] => o
            [17] => n
            [18] => :
            [19] => e
            [20] => d
            [21] => i
            [22] => t
            [23] => i
            [24] => o
            [25] => n
            [26] => _
            [27] => i
            [28] => n
            [29] => d
            [30] => e
            [31] => x
            [32] => ]
            [33] => 	
            [34] => 	
        )
)

Your templating layer is processing the output of that php code. The replacement is done after that code is executed. echo $data; really does output the string that preg_match_all is showing. It’s just that the templating layer later alters it.

You either need to:
-run preg_match_all after your templating layer has done the replacement.
-Have your templating layer do the replacement before executing that php code.

I don’t see how I would go about doing that. Can you write a code example as to what I might do?

Thanks for the help.

I’m not likely to be familiar with the cms/templating system you use, so no, I wouldn’t be able to do that even if I knew what you’re using.

@OP: You could always look into the code of the template system, to see how things are replaced. Then grab the links directly from their source.

Unfortunately, they don’t allow access to any of their system files.

Well that’s gonna be a bit of a problem…

How about you create a blank file with just the template command in it, and then read remotely that as if it’s an external file?

Actually that is my plan if I cant figure this out.

It is just bothering me that I cant figure out how to process the output of the template through the preg_match function.

It’s because the template etc is done after your code, so preg_match will only see the template instructions.

So I’m guessing reading it remotely is my only option…

Thanks for the help Jake!