{"id":107,"date":"2005-08-31T15:39:57","date_gmt":"2005-08-31T13:39:57","guid":{"rendered":"http:\/\/www.maratz.com\/blog\/archives\/2005\/08\/31\/perfect-third-party-ads\/"},"modified":"2006-04-06T20:55:02","modified_gmt":"2006-04-06T18:55:02","slug":"perfect-third-party-ads","status":"publish","type":"post","link":"https:\/\/www.maratz.com\/blog\/archives\/2005\/08\/31\/perfect-third-party-ads\/","title":{"rendered":"Perfect Third Party Ads"},"content":{"rendered":"<p>Everyone who ever worked on a high-profile web site with web standards know there\u2019s inevitable moment when the site has to go to its\u2019 new owners. More than often we fall in love with the site\u2019s tight structure and semantic markup. My heart breaks at that right moment when I have to insert a third-party advertisement script, which I know in advance is a tag soup. But can we do something about it? <!--more--><\/p>\n<h3>Lucky for Us, We Have Regular Expressions<\/h3>\n<p>With <a href=\"http:\/\/www.regular-expressions.info\/php.html\">regular expressions<\/a> and a bit of PHP we can clean HTML junk in the third-party scripts and turn it into nice, tidy markup. I assume that you have at least adequate knowledge of PHP.<\/p>\n<p>Let\u2019s see the typical piece of HTML we often get from an ad supplier:<\/p>\n<pre>&#60;style>\r\na { COLOR: RED; BACKGROUND: GREEN; }\r\na:hover { COLOR: YELLOW; BACKGROUND: ORANGE; }\r\n&#60;\/style>\r\n&#60;table border=0 width=100% background=#FFFFF>\r\n&#60;tr>\r\n&#60;td>&#60;a href=\"http:\/\/somesite.com\/\">ad link 1&#60;\/a>&#60;\/td>\r\n&#60;\/tr>\r\n&#60;tr>\r\n&#60;td>&#60;a href=\"http:\/\/somesite.com\/\">ad link 2&#60;\/a>&#60;\/td>\r\n&#60;\/tr>\r\n&#60;tr>\r\n&#60;td>&#60;a href=\"http:\/\/somesite.com\/\">ad link 3&#60;\/a>&#60;\/td>\r\n&#60;\/tr>\r\n&#60;\/table><\/pre>\n<p>The above is most likely to be inserted somewhere under vertical navigation or within the rest ugly advertisement kids. The <code>&#60style><\/code> tags shouldn\u2019t be anywhere else than inside the <code>&#60;head><\/code> tags. I also personally don\u2019t like that table, so we\u2019ll attempt to transform it into a much more appropriate unordered list. But I suppose that\u2019s just my weird taste.<\/p>\n<h3>Clean it While It\u2019s in the Buffer<\/h3>\n<p>I suppose if you\u2019re working on some content-driven, large-scale, high-profile web site, you output everything from a buffer, but that\u2019s a discussion for some other occasion. It is the best to use <a href=\"http:\/\/www.php.net\/ob_start\">output buffering<\/a> to clean bad markup before it\u2019s sent to the browser.<\/p>\n<h3>Careful Planning is the Key<\/h3>\n<p>First, let\u2019s make a  new structure. To make the HTML semantically correct we should place the above sample links into an unordered list. The final markup should look like the following:<\/p>\n<pre>&#60;div id=\"advertisementId\" class=\"ads\">\r\n&#60;ul>\r\n&#60;li>&#60;a href=\"http:\/\/somesite.com\/\">ad link 1&#60;\/a>&#60;\/li>\r\n&#60;li>&#60;a href=\"http:\/\/somesite.com\/\">ad link 2&#60;\/a>&#60;\/li>\r\n&#60;li>&#60;a href=\"http:\/\/somesite.com\/\">ad link 3&#60;\/a>&#60;\/li>\r\n&#60;\/ul>\r\n&#60;\/div><\/pre>\n<p>The CSS should be append to your main CSS file or better yet (and if the ad supplier permits this kind of modification of the ads), you or your team designer should style ad links according to the site\u2019s main look and feel. It\u2019s still very rarely the case, but lately it\u2019s getting better, especially with the clients who begin to understand that what attracts visitors is the web site\u2019s content.<\/p>\n<p>If you start replacing without the major plan, you\u2019ll probably end up spending too much of your valuable hours, which could have been spent on cross-browser debugging or accessibility improvements, to name a few.<\/p>\n<p>In PHP, strings are replaced with two very powerful functions: the <a href=\"http:\/\/www.php.net\/str_replace\"><code>str_replace()<\/code><\/a> and the <a href=\"http:\/\/www.php.net\/preg_replace\"><code>preg_replace()<\/code><\/a>. The former is useful for small chunks and it takes advantage over the later with its\u2019 speed. <code>preg_replace()<\/code> deals well with regular expression patterns, but is also more intensive for the server processor &#8211; it\u2019s something you don\u2019t want to play with on the popular web site. However, if applied carefully, it shouldn\u2019t affect the performance.<\/p>\n<h3>Step By Step Replacement<\/h3>\n<p>First thing\u2019s first &#8211; let\u2019s remove white space between HTML elements &#8211; it will save us a lot of trouble later:<\/p>\n<pre>$content = preg_replace('\/>(&#92;s|&#92;n|&#92;r)*&#60;si' , '>&#60;', $content);<\/pre>\n<p>The next step is also very simple &#8211; we\u2019ll remove everything within <code>&#60;style><\/code> and <code>&#60;\/style><\/code>, including those two.<\/p>\n<pre>$content = preg_replace('\/&#60;style.*?style>\/si', '', $content);<\/pre>\n<p>After we got rid of the improperly placed <code>&#60;style><\/code> element, we should remove <code>&#60;table><\/code> tags and place all those table rows into a division. At this point we can also define an id and a class attribute for that division and also add the unordered list for the list items.<\/p>\n<pre>$content = preg_replace('\/&#60;table.*?>(.*)?&#60;&#92;\/table>\/si', '&#60;div id=\"advertisementId\" class=\"ads\">&#60;ul>$1&#60;\/ul>&#60;\/div>', $content);<\/pre>\n<p>We simply pulled everything inside the <code>&#60;table>&#60;\/table><\/code> and pushed it into a <code>&#60;div id=\"advertisementId\" class=\"ads\">&#60;ul>&#60;\/ul>&#60;\/div><\/code>. This is stil pretty untasty tag soup, but the only thing that\u2019s left to be made is transforming each table row into a list item&#8230;<\/p>\n<pre>$content = preg_replace('\/&#60;tr>&#60;td>\/si', '&#60;li>', $content);\r\n$content = preg_replace('\/&#60;&#92;\/td>&#60;&#92;\/tr>\/si', '&#60;\/li>', $content);<\/pre>\n<p>&#8230; and there it is &#8211; a perfectly tight markup. Below is the complete code, which you can copy to a file with a <code>.php<\/code> extension and try it at the safety of your home or office:<\/p>\n<pre>&#60;?php\r\nfunction clean_HTML($content) {\r\n   $content = preg_replace('\/>(&#92;s|&#92;n|&#92;r)*&#60;si' , '>&#60;', $content);\r\n   $content = preg_replace('\/&#60;style.*?style>\/si', '', $content);\r\n   $content = preg_replace('\/&#60;table.*?>(.*)?&#60;&#92;\/table>\/si', '&#60;div id=\"advertisementId\" class=\"ads\">&#60;ul>$1&#60;\/ul>&#60;\/div>', $content);\r\n   $content = preg_replace('\/&#60;tr>&#60;td>\/si', '&#60;li>', $content);\r\n   $content = preg_replace('\/&#60;&#92;\/td>&#60;&#92;\/tr>\/si', '&#60;\/li>', $content);\r\n   return $content;\r\n}\r\nob_start('clean_HTML');   \r\n?>\r\n\r\n&#60;style>\r\na { COLOR: RED; BACKGROUND: GREEN; }\r\na:hover { COLOR: YELLOW; BACKGROUND: ORANGE; }\r\n&#60;\/style>\r\n&#60;table border=0 width=100% background=#FFFFF>\r\n&#60;TR>\r\n&#60;TD>&#60;a href=\"http:\/\/somesite.com\/\">ad link 1&#60;\/a>&#60;\/td>\r\n&#60;\/tr>\r\n&#60;tr>\r\n&#60;td>&#60;a href=\"http:\/\/somesite.com\/\">ad link 2&#60;\/a>&#60;\/td>\r\n&#60;\/tr>\r\n&#60;tr>\r\n&#60;td>&#60;a href=\"http:\/\/somesite.com\/\">ad link 3&#60;\/a>&#60;\/td>\r\n&#60;\/tr>\r\n&#60;\/table>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Everyone who ever worked on a high-profile web site with web standards know there\u2019s inevitable moment when the site has to go to its\u2019 new owners. More than often we fall in love with the site\u2019s tight structure and semantic markup. My heart breaks at that right moment when I have to insert a third-party [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/107"}],"collection":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}