{"id":7488,"date":"2010-08-06T10:07:02","date_gmt":"2010-08-06T17:07:02","guid":{"rendered":"http:\/\/wapreview.com\/?p=7488"},"modified":"2010-08-06T14:16:47","modified_gmt":"2010-08-06T21:16:47","slug":"how-to-keep-ads-from-breaking-your-mobile-pages","status":"publish","type":"post","link":"https:\/\/wapreview.com\/7488\/","title":{"rendered":"How To Keep Ads From Breaking Your Mobile Pages"},"content":{"rendered":"

\"XML<\/p>\n

I try to build mobile sites that work on everything from an old wml-only browsers to iPhones and the latest Android “Superphone”.\u00a0 It’s more work but I believe it’s worthwhile. Supporting more devices brings in more traffic and hopefully makes users of old devices smile when they find a site that actually works on their phones.<\/p>\n

I use adaptation to vary page and image sizes and MIME types to suit the capabilities of various devices.<\/p>\n

BlackBerrys, iPhones and most modern\u00a0 smartphones seem to work best with plain text\/html.<\/p>\n

But older phones expect XHTML-MP markup (WML for really old devices) together with the matching MIME types , “application\/vnd.wap.xhtml+xml” or “text\/vnd.wap.wml”.\u00a0 These XML based markup languages and XML MIME types cause most browsers to parse the page strictly.\u00a0 If the markup is not well formed and valid, the browser may display an error message rather than the page. You need to be sure your markup is valid.<\/p>\n

My pages are valid and work well in a wide range of browsers, at least until I introduce mobile ads into the mix. I’ve used most of the major mobile ad networks and they all at least occasionally serve ads that are not valid XML and cause pages to break.\u00a0 Some networks get it right most of the time, others never seem to. The most common errors are <img> tags that are missing the closing slash and naked ampersands (&)\u00a0 in URLs.\u00a0 The & character is reserved in XML and must represented using the entity &amp;<\/p>\n

If you use ads on pages that are served with an XML MIME type you need to clean up the ad’s markup.\u00a0 Fortunately there are libraries that make this easy. For PHP Tidy<\/a> is popular.\u00a0 Unfortunately it’s an optional extension that has to be compiled into the PHP exectable, which my shared hosting provider neglected to do.\u00a0 Instead I use htmLawed<\/a> which is a PHP script that should work with any host.\u00a0 Here’s some code that should clean up the ads from any provider.<\/p>\n

\/\/Include the htmLawed library
\nrequire_once(“htmLawed.php”);<\/p>\n

\/\/ Start output buffering
\nob_start();<\/p>\n

\/\/ execute the ad code and capture it’s output in the buffer
\ninclude (“AdNetworkCode.php”);<\/p>\n

\/\/ clear the buffer and capture the ad code in a variable
\n$ad = ob_get_clean();<\/p>\n

\/\/ Configure HtmLawed to produce valid xhtml.
\n$config = array(‘valid_xhtml’=>1);<\/p>\n

\/\/ Cleanup the ad code
\n$ad = htmLawed($ad, $config);<\/p>\n

\/\/ Output the cleaned ad to the page
\necho $ad;<\/p>\n

The code should be pretty self-explanatory.
\n“AdNetworkCode.php” is your ad network’s unmodified code. You can either “include” it as I did here or paste in the entire code block at this point.
\n“ob_start();” turns on PHP’s output buffering which captures the ad code’s output in an internal buffer rather than echoing it to the page.
\n“$ad = ob_get_clean();” stores the output of the ad script in a variable so we can process in with HtmLawed. Using output buffering eliminates the need to modify the ad code which is prohibited by most ad network’s terms of service.
\n
\nRelated Post<\/em>:
Validation is Your Friend<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

I try to build mobile sites that work on everything from an old wml-only browsers to iPhones and the latest Android “Superphone”.\u00a0 It’s more work but I believe it’s worthwhile. Supporting more devices brings in more traffic and hopefully makes users of old devices smile when they find a site that actually works on their phones. I use adaptation to vary page and image sizes and MIME types to suit the capabilities of various devices. BlackBerrys, iPhones and most modern\u00a0 … Continue reading →<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":14683,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[273,219,1096],"_links":{"self":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts\/7488"}],"collection":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/comments?post=7488"}],"version-history":[{"count":4,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts\/7488\/revisions"}],"predecessor-version":[{"id":7491,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/posts\/7488\/revisions\/7491"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/media\/14683"}],"wp:attachment":[{"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/media?parent=7488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/categories?post=7488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wapreview.com\/wp-json\/wp\/v2\/tags?post=7488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}