/mvc/components/filter
[return to app]1
<?php
2 /**
3 * User-entered data filter
4 */
5 class filterComponent {
6 /**
7 * Generates a URL-safe SEO-friendly string from any text string; long strings are truncated to 100 chars
8 *
9 * @param string $str
10 * @return string
11 */
12 public function urlString($str) {
13 $str = preg_replace('/-{2,}/', '-', preg_replace('/[^a-z0-9-]/', '-', str_replace('_', '-',
strtolower($str))));
14 return trim(substr($str, 0, 100), '-');
15 }
16
17 /**
18 * Returns a string containing only numeric digits
19 *
20 * @param string $data
21 * @param bool $decimal
22 * @return float
23 */
24 public function getNumbersOnly($data, $decimal = false) {
25 return preg_replace(($decimal ? '/[^\d\.]/' : '/\D/'), '', $data);
26 }
27
28 /**
29 * Replace deprecated font-sizes with accessibility-friendly sizes in CSS format
30 *
31 * @param mixed $size
32 * @return string
33 */
34 protected function _fontsizereplace($size) {
35 $sizes = array(-2 => 'xx-small', -1 => 'x-small', '+1' => 'small', '+2' => 'medium', '+3' => 'large',
36 '+4' => 'x-large', 0 => 'xx-small', '1' => 'x-small', '2' => 'small', '3' => 'medium',
37 '4' => 'large', '5' => 'x-large', '6' => 'x-large', '7' => 'x-large', '8' => 'x-large');
38 if (isset($sizes[$size[2]])) {
39 $size[2] = $sizes[$size[2]];
40 }
41 return '<span style="font-size:' . $size[2] . ';">';
42 }
43
44 /**
45 * Cleans up messy broken HTML and returns pristine code that is XHTML-valid
46 *
47 * Requires the PHP Tidy extension to be enabled, although it will work without it but clean up will
48 * be severly limited and it will not convert broken HTML into valid XHTML
49 *
50 * @param string $html
51 * @return string
52 */
53 public function cleanHtml($html) {
54 $html = preg_replace('/(<font .*?color="(.*?)".*?'.'>)/mi', '$1<span style="color:$2;">', $html);
55 $html = preg_replace_callback('/(<font .*?size="(.*?)".*?'.'>)/mi', array($this, '_fontsizereplace'),
$html);
56 $deprecated = array('</font>' => '</span>',
57 '<center>' => '<div style="text-align:center;">', '</center>' => '</div>',
58 '<strike>' => '<span style="text-decoration:line-through;">', '</strike>' =>
'</span>',
59 '<s>' => '<span style="text-decoration:line-through;">', '</s>' => '</span>',
60 '<u>' => '<span style="text-decoration:underline;">', '</u>' => '</span>');
61 $html = str_ireplace(array_keys($deprecated), array_values($deprecated), $html);
62 if (extension_loaded('tidy')) {
63 $tidy = new tidy;
64 $phpopen = '|TIDY_PHP_OPEN|';
65 $phpclose = '|TIDY_PHP_CLOSE|';
66 $html = str_replace(array('<?', '?>'), array($phpopen, $phpclose), $html);
67 $tidy->parseString($html, array('show-body-only' => true, 'output-xhtml' => true, 'word-2000' =>
true,
68 'wrap' => 0, 'wrap-php' => false, 'hide-comments' => true,
69 'drop-font-tags' => true, 'logical-emphasis' => true));
70 $tidy->cleanRepair(); //the next line strips out placeholders and casts to string in the process
71 $html = trim(str_replace(array($phpopen, $phpclose), array('<?', '?>'), $tidy));
72 }
73 $html = preg_replace('/(<img (.*?) align="(.*?)"(.*?)'.'>)/mi', '<img $2 style="float:$3;"$4>', $html);
74 return $html;
75 }
76 }