Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | Text Helper |
| 3 | ########### |
| 4 | |
| 5 | The Text Helper file contains functions that assist in working with |
| 6 | text. |
| 7 | |
| 8 | .. contents:: Page Contents |
| 9 | |
| 10 | Loading this Helper |
| 11 | =================== |
| 12 | |
| 13 | This helper is loaded using the following code |
| 14 | |
| 15 | :: |
| 16 | |
| 17 | $this->load->helper('text'); |
| 18 | |
| 19 | The following functions are available: |
| 20 | |
| 21 | word_limiter() |
| 22 | ============== |
| 23 | |
| 24 | Truncates a string to the number of **words** specified. Example:: |
| 25 | |
| 26 | $string = "Here is a nice text string consisting of eleven words."; |
| 27 | $string = word_limiter($string, 4); |
| 28 | // Returns: Here is a nice… |
| 29 | |
| 30 | The third parameter is an optional suffix added to the string. By |
| 31 | default it adds an ellipsis. |
| 32 | |
| 33 | character_limiter() |
| 34 | =================== |
| 35 | |
| 36 | Truncates a string to the number of **characters** specified. It |
| 37 | maintains the integrity of words so the character count may be slightly |
| 38 | more or less then what you specify. Example |
| 39 | |
| 40 | :: |
| 41 | |
| 42 | $string = "Here is a nice text string consisting of eleven words."; |
| 43 | $string = character_limiter($string, 20); |
| 44 | // Returns: Here is a nice text string… |
| 45 | |
| 46 | The third parameter is an optional suffix added to the string, if |
| 47 | undeclared this helper uses an ellipsis. |
| 48 | |
| 49 | ascii_to_entities() |
| 50 | =================== |
| 51 | |
| 52 | Converts ASCII values to character entities, including high ASCII and MS |
| 53 | Word characters that can cause problems when used in a web page, so that |
| 54 | they can be shown consistently regardless of browser settings or stored |
| 55 | reliably in a database. There is some dependence on your server's |
| 56 | supported character sets, so it may not be 100% reliable in all cases, |
| 57 | but for the most part it should correctly identify characters outside |
| 58 | the normal range (like accented characters). Example |
| 59 | |
| 60 | :: |
| 61 | |
| 62 | $string = ascii_to_entities($string); |
| 63 | |
| 64 | entities_to_ascii() |
| 65 | =================== |
| 66 | |
| 67 | This function does the opposite of the previous one; it turns character |
| 68 | entities back into ASCII. |
| 69 | |
| 70 | convert_accented_characters() |
| 71 | ============================= |
| 72 | |
| 73 | Transliterates high ASCII characters to low ASCII equivalents, useful |
| 74 | when non-English characters need to be used where only standard ASCII |
| 75 | characters are safely used, for instance, in URLs. |
| 76 | |
| 77 | :: |
| 78 | |
| 79 | $string = convert_accented_characters($string); |
| 80 | |
| 81 | This function uses a companion config file |
| 82 | `application/config/foreign_chars.php` to define the to and from array |
| 83 | for transliteration. |
| 84 | |
| 85 | word_censor() |
| 86 | ============= |
| 87 | |
| 88 | Enables you to censor words within a text string. The first parameter |
| 89 | will contain the original string. The second will contain an array of |
| 90 | words which you disallow. The third (optional) parameter can contain a |
| 91 | replacement value for the words. If not specified they are replaced with |
| 92 | pound signs: ####. Example |
| 93 | |
| 94 | :: |
| 95 | |
| 96 | $disallowed = array('darn', 'shucks', 'golly', 'phooey'); |
| 97 | $string = word_censor($string, $disallowed, 'Beep!'); |
| 98 | |
| 99 | highlight_code() |
| 100 | ================ |
| 101 | |
| 102 | Colorizes a string of code (PHP, HTML, etc.). Example:: |
| 103 | |
| 104 | $string = highlight_code($string); |
| 105 | |
| 106 | The function uses PHP's highlight_string() function, so the colors used |
| 107 | are the ones specified in your php.ini file. |
| 108 | |
| 109 | highlight_phrase() |
| 110 | ================== |
| 111 | |
| 112 | Will highlight a phrase within a text string. The first parameter will |
| 113 | contain the original string, the second will contain the phrase you wish |
| 114 | to highlight. The third and fourth parameters will contain the |
| 115 | opening/closing HTML tags you would like the phrase wrapped in. Example |
| 116 | |
| 117 | :: |
| 118 | |
| 119 | $string = "Here is a nice text string about nothing in particular."; |
| 120 | $string = highlight_phrase($string, "nice text", '<span style="color:#990000">', '</span>'); |
| 121 | |
| 122 | The above text returns: |
| 123 | |
| 124 | Here is a nice text string about nothing in particular. |
| 125 | |
| 126 | word_wrap() |
| 127 | =========== |
| 128 | |
| 129 | Wraps text at the specified **character** count while maintaining |
| 130 | complete words. Example |
| 131 | |
| 132 | :: |
| 133 | |
| 134 | $string = "Here is a simple string of text that will help us demonstrate this function."; |
| 135 | echo word_wrap($string, 25); |
| 136 | |
| 137 | // Would produce: Here is a simple string of text that will help us demonstrate this function |
| 138 | |
| 139 | ellipsize() |
| 140 | =========== |
| 141 | |
| 142 | This function will strip tags from a string, split it at a defined |
| 143 | maximum length, and insert an ellipsis. |
| 144 | |
| 145 | The first parameter is the string to ellipsize, the second is the number |
| 146 | of characters in the final string. The third parameter is where in the |
| 147 | string the ellipsis should appear from 0 - 1, left to right. For |
| 148 | example. a value of 1 will place the ellipsis at the right of the |
| 149 | string, .5 in the middle, and 0 at the left. |
| 150 | |
| 151 | An optional forth parameter is the kind of ellipsis. By default, |
| 152 | … will be inserted. |
| 153 | |
| 154 | :: |
| 155 | |
| 156 | $str = 'this_string_is_entirely_too_long_and_might_break_my_design.jpg'; |
| 157 | echo ellipsize($str, 32, .5); |
| 158 | |
| 159 | Produces: |
| 160 | |
| 161 | :: |
| 162 | |
| 163 | this_string_is_e…ak_my_design.jpg |
| 164 | |