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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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:
    Code:
    <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 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:
    Code:
    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????

    Any thoughts anyone?

    Thanks.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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

  • #3
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    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 Code:
    <?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:

    Code:
    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:

    PHP Code:
    foreach ($data as $line) {
        echo 
    "Line: " $line

    Last edited by xconspirisist; 03-30-2010 at 08:40 PM.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • #5
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    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
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by xconspirisist View Post
    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
    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.

  • #7
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xconspirisist View Post
    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
    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.

  • #8
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    See for yourself!: http://tydus.net/tst.php
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #9
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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):

    Code:
    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.
    Last edited by infekt; 03-30-2010 at 11:15 PM.

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Bugger. Now I'm well peeved. That's pretty much along the lines of what I suggested in the post I deleted.

  • #11
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Ah, then your original question was quite wrong. Fix yer templating engine!
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #12
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • Users who have thanked MattF for this post:

    infekt (03-31-2010)

  • #14
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    Code:
    $data=get_file_contents('http://mysite.com/secondpage.html');
    This worked. So I guess it'll have to be good enough.

    Thanks again.

  • #15
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Seems a rather roundabout way of doing it though. Can you not directly run the template parser on a string containg the token instead?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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