...

View Full Version : Variable Data Mystery



infekt
03-30-2010, 07:09 PM
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/>

Here is ALL OF THE CODE:



<?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] =>
)
)


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???? :confused:

Any thoughts anyone?

Thanks.

mlseim
03-30-2010, 08:31 PM
preg_match_all stores matched results into an array.

No mystery, it is what it is:
http://php.net/manual/en/function.preg-match-all.php

xconspirisist
03-30-2010, 08:32 PM
Ah. Well matches should be passed as a reference, but you have not declared it, so I figure you are using it in a bit of the wrong way.

I think you may find the following more to what you want:


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

$data = explode("\n", ob_get_contents());


Which gives you a nice simple one dimensional array that looks like this:



Array (
[0] => "<a href="/special_section/4/">Answer Book</a><br/><br/>"
[1] => "<a href="/special_section/9/">Home Experts</a><br/><br/>"
[2] => "<a href="/special_section/11/">Young at Heart</a><br/><br/>"
...
)


Then you can iterate over each link and print it out like this:



foreach ($data as $line) {
echo "Line: " . $line;
}

MattF
03-30-2010, 08:35 PM
The O.P is referring to the fact that preg_match is seeing the template placeholder whilst echo is seeing the contents which replace the placeholder.


p.s: Incase you're wondering, I missed the point first time around and hence deleted my post. :D

xconspirisist
03-30-2010, 08:43 PM
Nah, there must be a problem between his chair and the keyboard - the code example he posted simply does not produce the output he posted :D

MattF
03-30-2010, 08:53 PM
Nah, there must be a problem between his chair and the keyboard - the code example he posted simply does not produce the output he posted :D

Probably. That's why I deleted my original post. I couldn't see any reason why $data would differ between the two, so I erred on the side of caution. :D

infekt
03-30-2010, 10:30 PM
Nah, there must be a problem between his chair and the keyboard - the code example he posted simply does not produce the output he posted :D

Yeah, I just decided to alter the output of my code for the hell of it. Thought maybe I could help generate some ad impressions for this lovely website making an insolvable post. :thumbsup:

xconspirisist
03-30-2010, 10:55 PM
See for yourself!: http://tydus.net/tst.php

infekt
03-30-2010, 11:12 PM
xconspirisist, you are missing an important aspect. you are not in the CMS environment I am in nor using the same data source.

Thanks for working up an example though.

But I finally see whats going on now....

My $data variable never contained the html. It only contained the template token. And as soon as I echoed out the $data var, which was actually "[special_section:edition_index]", then my CMS environment rendered it to the html.

Which then explains why im getting the following when I print_r($matches):


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] =>
)
)


Make sense?

So now I need to figure how to actually get the html into a variable, which can then be passed to the preg_match_all() function.

MattF
03-30-2010, 11:19 PM
Bugger. Now I'm well peeved. That's pretty much along the lines of what I suggested in the post I deleted. :D

xconspirisist
03-30-2010, 11:30 PM
Ah, then your original question was quite wrong. Fix yer templating engine!

infekt
03-30-2010, 11:54 PM
My original question was wrong? Ha ok.

Also, its not my templating engine. I'd love to never have to work in this POS CMS ever again. But at the moment.. it helps pay the bills.

Thanks for the help guys.

MattF
03-31-2010, 12:46 AM
You need to capture the output after it has been parsed by the template system, not before. Without knowing the specifics of your code, however, there's no feasible way to suggest a solution.

infekt
03-31-2010, 12:55 AM
What I ended up doing was creating a second page that solely has the template token, and just renders out the html.

Then on my original page, I am using:

$data=get_file_contents('http://mysite.com/secondpage.html');

This worked. So I guess it'll have to be good enough.

Thanks again.

MattF
03-31-2010, 01:02 AM
Seems a rather roundabout way of doing it though. Can you not directly run the template parser on a string containg the token instead?

infekt
03-31-2010, 04:21 PM
This system does not give me direct access to the template parser. The CMS providers are a bit authoritarian.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum