include( $modx->config[ "base_path" ] . "/web_assets/classes/phpQuery.php" ); $doc = phpQuery::newDocument ($output); $middle = strlen( strip_tags( $doc->find( ".content") ) ) / 1.8; $quarter = $middle / 2; $els = pq('.content > *'); $cur=0; $ad_shown = 0; $related_shown = 0; foreach($els as $i) { $s = strip_tags( pq($i) ); $len = strlen( $s ); $cur += $len; if ( $len > 100 ) { if ( !$ad_shown && $cur > $middle ) { pq($i)->append( " <div id='div-gpt-ad-1446129297051-0'> <script type=\"text/javascript\">googletag.cmd.push(function() { googletag.display('div-gpt-ad-1446129297051-0'); }); </script></div>" ); $ad_shown = 1; } if ( ( $related_shown < 1 && $cur > $quarter ) || ( $related_shown < 2 && $cur > ( $quarter + $middle ) ) ) { if ( isset( $related_s[ $related_shown ] ) ) { $st = $related_s[ $related_shown ]; $st["image_name"] = pathinfo( $st["image_name"] ); $st["image_name"] = $st["image_name"][ "filename"]; $url = strtolower( $st["section_cd"] ) . "/" . strtolower( $st[ "category_cd" ] ) . "/" . $st[ "url" ]; pq($i)->append( "<p class='related-inline'" . ( stristr( $output, "blockquote" ) ? " style='float:left;'" : "" ) . "><a href='https://" . $_SERVER['HTTP_HOST'] . "/" . $url . "' onclick=\"return YouInc.trackLink(this,['CTA Click','Inline Related Story', '" . $url ."']);\" alt='" . $st[ "title" ] . "' title='" . $st[ "title" ] . "' class='image-link'><img src='/cached_images/stories/full/" . $st["image_name"] . "-110x46.jpg' /></a><a href='https://" . $_SERVER['HTTP_HOST'] . "/" . $url."' class='title-link' onclick=\"return YouInc.trackLink(this,['CTA Click','Inline Related Story', '" . $url. "']);\"><strong>Related:</strong> " . $st[ "title" ] . "</a></p>" ); $related_shown ++; } } } } $output = $doc;
PHP Query .. Amazing little library
Bookmark the permalink.