{"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":"
<\/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 &<\/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 \/\/ Start output buffering \/\/ execute the ad code and capture it’s output in the buffer \/\/ clear the buffer and capture the ad code in a variable \/\/ Configure HtmLawed to produce valid xhtml. \/\/ Cleanup the ad code \/\/ Output the cleaned ad to the page The code should be pretty self-explanatory. 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
\nrequire_once(“htmLawed.php”);<\/p>\n
\nob_start();<\/p>\n
\ninclude (“AdNetworkCode.php”);<\/p>\n
\n$ad = ob_get_clean();<\/p>\n
\n$config = array(‘valid_xhtml’=>1);<\/p>\n
\n$ad = htmLawed($ad, $config);<\/p>\n
\necho $ad;<\/p>\n
\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":"