ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

escaping {body} for use in PHP

December 30, 2009 8:34pm

Subscribe [2]
  • #1 / Dec 30, 2009 8:34pm

    Dave Rau

    85 posts

    I’m trying to pull images from the {body} of blog posts. How can I properly escape {body} for use in either XML parsing or just preg_match?

    // can use this to grab body, but it won't parse
    <?php
    ob_start();
    ?>{body}<?php
    $body = ob_get_contents();
    ob_end_clean();
    
    // this won't parse either
    $body = '{body}';
    $body = htmlentities($body);
    
    // but this works
    $body = '<a href="http://www.test.com/">http://www.x.com/thumbnails/x.jpg</a>';
    
    preg_match_all('/<img[^>]+>/i',$body, $result); 
    
    $doc = new DOMDocument();
    $doc->loadHTML($body);
    $xml = simplexml_import_dom($doc);
    $images = $xml->xpath('//img');
    foreach ($images as $img) { 
        echo $img["src"];
    }
    
    ?>

    The html I’m expecting in {body} doesn’t seem to be funky, so what gives? How can I escape {body} so that it will properly be parsed for either preg_matching or the xml parser?

    Thanks!
    -dave

  • #2 / Dec 30, 2009 9:17pm

    Herb

    224 posts

    Do you have php parsing on input or output?

    On input you will be grabbing the {body} tag, not its content.
    On output you should be grabbing the content of the body.

    Is this all performed within the same template?

    PHP variables set in one template only exist in that template.

  • #3 / Dec 30, 2009 10:02pm

    Dave Rau

    85 posts

    Herb, I had parsing set to input, but it seems like I need both to do this; any ideas? Thanks.

    <?php
    ob_start();
    ?>{body}<?php
    $body = ob_get_contents();
    ob_end_clean();
    $doc = new DOMDocument();
    @$doc->loadHTML($body);
    $xml = simplexml_import_dom($doc);
    $images = $xml->xpath('//img');
    foreach ($images as $img) { 
        $thumb = $img["src"];
        ?>
        {exp:imgsizer:size src="<?=$thumb?>" width="194" alt="{title}"}
        <?php
    }
    ?>
  • #4 / Dec 30, 2009 11:25pm

    Herb

    224 posts

    A plug-in is probably called for.

    You would need a function to capture the {body}.  Then you can do a regex to extract the src into an array.  next you would need a function to do the foreach and pass the result to {exp:imgsizer} in an embedded template.

    Each time you call any plug-in ee creates a new instance, so you store the array in $SESS->cache so it is available the next time the plug-in is called in your template and allows you to deal with the parse order issue.

    Plug-ins are quite simple to do.

  • #5 / Dec 31, 2009 9:41am

    Dave Rau

    85 posts

    Well I ended up using this second plugin; not ideal but it cuts to the chase.
    http://www.digitaldelivery.co.uk/downloads/dd_autothumb/

    I’ll investigate a plugin later, but thanks for all your help Herb.
    -dave

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases