Я получаю контент через Amazon Web Services (например, описания продуктов).Поскольку контент от Amazon часто размечается очень плохо, в конечном итоге он портит макет моих веб-страниц.Итак, я придумала функцию «санации» содержимого с помощью HTML Tidy.
Странная вещь в том, что когда я тестирую его отдельно от приложения, кажется, что все работает нормально.Но в моем приложении (работающем на CodeIgniter) функция, кажется, возвращает нечетные символы.
Код ниже - мой тестовый скрипт.Он выводит то, что мне нужно.
В своем приложении я извлекаю описание из базы данных, очищаю его, а затем отображаю на своей веб-странице.Например, после очистки, 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>