アマゾンウェブサービスを介してコンテンツを取得しています(製品の説明など)。アマゾンからのコンテンツはしばしば非常に不十分にマークアップされるので、それは私のウェブページのレイアウトを台無しにすることになります。そこで、HTMLTidyを使用してコンテンツを「サニタイズ」する機能を考案しました。
奇妙なことに、アプリケーションとは別にテストすると、すべてが正常に機能しているように見えます。しかし、私のアプリケーション(CodeIgniterで実行)では、関数は奇妙な文字を返すようです。
以下のコードは私のテストスクリプトです。必要だと思うものを出力しています。
私のアプリケーションでは、データベースから説明を取得し、サニタイズしてから、Webページに表示します。たとえば、サニタイズ後document’s
(以下の例でこの単語を見ることができます)はdocument’s
(ここでも、実際のアプリケーションでのみ、テストコードではありません。両方の機能は同じです)になります。
なぜ何かアイデアはありますか?これが私のテスト関数です:
$amazon_content = <<<AMAZON
JavaScript is the brains of your Web page—it enables you to modify a document’s structure, styling, and content in response to user actions without requesting new pages from the server. Scriptin' with JavaScript and Ajax teaches you how to master this powerful and elegant language so you can develop intuitive user interactions that take the user experience to new levels of sophistication and responsiveness.<br><br>Today’s application-like Web experiences (such as Salesforce.com and Google Maps) and Web 2.0 sites (such as Flickr.com and Twitter) are powered by JavaScript and Ajax. Using the techniques shown in this book, you will be able to start creating similar experiences in the sites you design.<br><br>Scriptin' with JavaScript and Ajax will teach you how to:<br><ul><li>Start developing with JavaScript fast!</li></ul><ul><li>Write lightweight but powerful object-oriented code </li></ul><ul><li>Modify the Document Object Model </li></ul><ul><li>“Progressively enhance” your pages with JavaScript to provide the highest levels of accessibility to all users</li></ul><ul><li>Learn sophisticated techniques for making your pages respond to user actions</li></ul><ul><li>Use the downloadable Scriptin’ library of helper functions to speed development and ensure cross-browser compatibility</li></ul><ul><li>Use Ajax scripting techniques to update specific areas of the page with data from the server</li></ul><ul><li>Create powerful interface interactions, such as sliding panels and tree menus</li></ul><ul><li>Evaluate frameworks such as jQuery and Prototype to find the best one for your needs</li></ul><ul><li>Build an online application that looks and responds like a regular desktop application</li></ul><ul><li>Easily adapt the Scriptin’ code examples for use in your own projects—download them at www.scriptinwithajax.com</li></ul><br>
AMAZON;
echo '<textarea cols="150" rows="12">' . $amazon_content . '</textarea>';
echo '<textarea cols="150" rows="12">' . get_sanitized_amazon_content($amazon_content) . '</textarea>';
echo get_sanitized_amazon_content($amazon_content);
function get_sanitized_amazon_content($amazon_content)
{
$tidy_config = array(
'bare' => TRUE,
'clean' => TRUE,
'drop-empty-paras' => TRUE,
'drop-font-tags' => TRUE,
'drop-proprietary-attributes' => TRUE,
'enclose-text' => TRUE,
'fix-backslash' => TRUE,
'fix-bad-comments' => TRUE,
'fix-uri' => TRUE,
'hide-comments' => TRUE,
'hide-endtags' => TRUE,
'logical-emphasis' => TRUE,
'lower-literals' => TRUE,
'merge-divs' => TRUE,
'output-xhtml' => TRUE,
'quote-ampersand' => TRUE,
'quote-marks' => TRUE,
'show-body-only' => TRUE,
'word-2000' => TRUE
);
$tidy = new tidy();
$sanitized_amazon_markup = $tidy->repairString($amazon_content, $tidy_config);
// Replace carriage returns, line feeds, tabs with single space
$sanitized_amazon_markup = preg_replace('/\r|\n|\t/', ' ', $sanitized_amazon_markup);
// Removes unnecessary tags
// TODO: get complete list; put in an array
$sanitized_amazon_markup = strip_tag($sanitized_amazon_markup, 'div');
$sanitized_amazon_markup = strip_tag($sanitized_amazon_markup, 'span');
// Replace double spaces with single space
$sanitized_amazon_markup = preg_replace('/ {2,}/i', ' ', $sanitized_amazon_markup);
// Remove leading and trailing space
$sanitized_amazon_markup = trim($sanitized_amazon_markup);
return $sanitized_amazon_markup;
}
function strip_tag($tagged_content, $tag_name)
{
return preg_replace('%<[ \t\r\n]*/?[ \t\r\n]*' . $tag_name . '.*?>%i', '', $tagged_content);
}
アップデート:
これは私が私のアプリケーションで得るものです:
<p>JavaScript is the brains of your Web pageâ€"it enables you to modify a document’s structure, styling, and content in response to user actions without requesting new pages from the server. Scriptin' with JavaScript and Ajax teaches you how to master this powerful and elegant language so you can develop intuitive user interactions that take the user experience to new levels of sophistication and responsiveness.<br /> <br /> Today’s application-like Web experiences (such as Salesforce.com and Google Maps) and Web 2.0 sites (such as Flickr.com and Twitter) are powered by JavaScript and Ajax. Using the techniques shown in this book, you will be able to start creating similar experiences in the sites you design.<br /> <br /> Scriptin' with JavaScript and Ajax will teach you how to:<br /></p> <ul> <li>Start developing with JavaScript fast!</li> </ul> <ul> <li>Write lightweight but powerful object-oriented code</li> </ul> <ul> <li>Modify the Document Object Model</li> </ul> <ul> <li>“Progressively enhance†your pages with JavaScript to provide the highest levels of accessibility to all users</li> </ul> <ul> <li>Learn sophisticated techniques for making your pages respond to user actions</li> </ul> <ul> <li>Use the downloadable Scriptin’ library of helper functions to speed development and ensure cross-browser compatibility</li> </ul> <ul> <li>Use Ajax scripting techniques to update specific areas of the page with data from the server</li> </ul> <ul> <li>Create powerful interface interactions, such as sliding panels and tree menus</li> </ul> <ul> <li>Evaluate frameworks such as jQuery and Prototype to find the best one for your needs</li> </ul> <ul> <li>Build an online application that looks and responds like a regular desktop application</li> </ul> <ul> <li>Easily adapt the Scriptin’ code examples for use in your own projectsâ€"download them at www.scriptinwithajax.com</li> </ul> <p><br /></p>
これは、アプリケーションの外部で取得するものです。
<p>JavaScript is the brains of your Web page-it enables you to modify a document's structure, styling, and content in response to user actions without requesting new pages from the server. Scriptin' with JavaScript and Ajax teaches you how to master this powerful and elegant language so you can develop intuitive user interactions that take the user experience to new levels of sophistication and responsiveness.<br /> <br /> Today's application-like Web experiences (such as Salesforce.com and Google Maps) and Web 2.0 sites (such as Flickr.com and Twitter) are powered by JavaScript and Ajax. Using the techniques shown in this book, you will be able to start creating similar experiences in the sites you design.<br /> <br /> Scriptin' with JavaScript and Ajax will teach you how to:<br /></p> <ul> <li>Start developing with JavaScript fast!</li> </ul> <ul> <li>Write lightweight but powerful object-oriented code</li> </ul> <ul> <li>Modify the Document Object Model</li> </ul> <ul> <li>"Progressively enhance" your pages with JavaScript to provide the highest levels of accessibility to all users</li> </ul> <ul> <li>Learn sophisticated techniques for making your pages respond to user actions</li> </ul> <ul> <li>Use the downloadable Scriptin' library of helper functions to speed development and ensure cross-browser compatibility</li> </ul> <ul> <li>Use Ajax scripting techniques to update specific areas of the page with data from the server</li> </ul> <ul> <li>Create powerful interface interactions, such as sliding panels and tree menus</li> </ul> <ul> <li>Evaluate frameworks such as jQuery and Prototype to find the best one for your needs</li> </ul> <ul> <li>Build an online application that looks and responds like a regular desktop application</li> </ul> <ul> <li>Easily adapt the Scriptin' code examples for use in your own projects-download them at www.scriptinwithajax.com</li> </ul> <p><br /></p>