├── package.json ├── system └── expressionengine │ └── third_party │ └── streeng │ ├── addon.setup.php │ └── pi.streeng.php ├── .gitignore ├── README.md └── LICENSE /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "schema_version": "1.0", 3 | "name" : "streeng", 4 | "label": "Streeng", 5 | "version": "1.10.0", 6 | "types": ["plugin"], 7 | "paths": { 8 | "system": [ 9 | "system/expressionengine/third_party/streeng" 10 | ] 11 | } 12 | } -------------------------------------------------------------------------------- /system/expressionengine/third_party/streeng/addon.setup.php: -------------------------------------------------------------------------------- 1 | STREENG_AUTHOR, 14 | 'author_url' => STREENG_AUTHOR_URL, 15 | 'description' => STREENG_DESC, 16 | 'docs_url' => STREENG_DOCS_URL, 17 | 'name' => STREENG_NAME, 18 | 'namespace' => 'Caddis\Streeng', 19 | 'version' => STREENG_VER 20 | ); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #------------------------------------ 2 | # Tools 3 | #------------------------------------ 4 | 5 | .bundle 6 | .idea/ 7 | .komodotools 8 | .project 9 | .settings/ 10 | .tern-project 11 | .tmproj 12 | .vscode/ 13 | *.esproj 14 | *.komodoproject 15 | *.sublime-* 16 | browserstack.js 17 | codekit-config.json 18 | prepros.json 19 | 20 | 21 | #------------------------------------ 22 | # System 23 | #------------------------------------ 24 | 25 | .cache 26 | .DS_Store 27 | .tmp 28 | *.diff 29 | *.err 30 | *.lock 31 | *.log 32 | *.orig 33 | *.rej 34 | *.swo 35 | *.swp 36 | *.vi 37 | error_log 38 | Thumbs.db 39 | 40 | 41 | #------------------------------------ 42 | # Gitkeep 43 | #------------------------------------ 44 | 45 | !.gitkeep -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Streeng 1.10.0 2 | 3 | Perform common operations on strings such as changing case, truncating, finding/replacing, repeating, encoding/decoding, generating slugs, and more. 4 | 5 | ## Quick Tag Reference (full documentation below) 6 | 7 | ``` 8 | allowed="p|span|a" - pass "none" to strip all tags or a pipe (|) delimited list of allowed tags (default = allow all) 9 | find="string1" - string to find (separate multiple values with the explode variable which defaults to "|", accepts regex) 10 | replace="string2" - string to replace found string (default = "") 11 | insensitive="yes" - toggle case sensitivity when finding a string (default = "no") 12 | explode="|" - string to split find/replace parameters with 13 | trim="left" - left, right, or both (default = "both") 14 | encode="yes" - HTML encode the string (default = "no") 15 | decode="yes" - HTML decode the string (default = "no") 16 | url="yes" - URL encode the string (default = "no", set to "raw" for raw encoding) 17 | capitalize="yes" - capitalize the first character of the string (default = "no") 18 | title="yes" - capitalize the first character of every word (default = "no") 19 | lower="yes" - convert the string to lower case (default = "no") 20 | upper="yes" - convert the string to upper case (default = "no") 21 | characters="10" - number of characters to truncate the string to (default = unlimited) 22 | words="10" - number of words to truncate the string to (default = unlimited) 23 | append="..." - if truncated append this to the end of the string (default = "…") 24 | slug="yes" - convert the string to a slug (default = "no") 25 | separator="_" - separator for slug (default = "-") 26 | repeat="3" - number of times to repeat the string, great for prototyping (default = 0) 27 | count="|" - return number of substring instances of a supplied string (default = false) 28 | splits="|" - return number of exploded values from a supplied string (default = false) 29 | typography="all|xhtml|br|lite" - Use ExpressionEngine's typography class 30 | ``` 31 | 32 | *** 33 | 34 | ## Parameters 35 | 36 | And here is more detailed information on the parameters available for the {exp:streeng} tag pair. 37 | 38 | ### Stripping HTML Tags 39 | 40 | ``` 41 | allowed="none" 42 | ``` 43 | 44 | or 45 | 46 | ``` 47 | allowed="p|span|a" 48 | ``` 49 | 50 | This will strip either all (if set to `allowed="none"`) or the desired HTML tags from the string enclosed by the tag pair. If this parameter is not set the default is to allow all. 51 | 52 | Example: 53 | 54 | ``` 55 | {exp:streeng allowed="none"} 56 |
Our plans for world domination are none of your concern. So go mind your own business!
57 | {/exp:streeng} 58 | ``` 59 | Result: 60 | 61 | ``` 62 | Our plans for world domination are none of your concern. So go mind your own business! 63 | ``` 64 | 65 | Example: 66 | 67 | ``` 68 | {exp:streeng allowed="a"} 69 |Our plans for world domination are none of your concern. So go mind your own business!
70 | {/exp:streeng} 71 | ``` 72 | 73 | Result: 74 | 75 | ``` 76 | Our plans for world domination are none of your concern. So go mind your own business! 77 | ``` 78 | 79 | ### Find and Replace 80 | 81 | ``` 82 | find="string1|string2" 83 | replace="string3" 84 | insensitive="yes" 85 | explode="|" — default: | 86 | ``` 87 | 88 | `find=""` accepts a pipe delimited list of strings to find and replace with the contents of `replace=""`. Allows you to find all instances of a string and replace them with your desired text, or you can set replace="" to simply remove the matching string. 89 | 90 | Optionally specify case sensitivity. Default is not case sensitive. 91 | 92 | Optionally specify the explode parameter. Default is pipe. 93 | 94 | You can also use Regex in your `find=""` parameter. 95 | 96 | Example: 97 | 98 | ``` 99 | {exp:streeng find="well under way" replace="none of your concern"} 100 | Our plans for world domination are well under way. 101 | {/exp:streeng} 102 | ``` 103 | 104 | Result: 105 | 106 | ``` 107 | Our plans for world domination are none of your concern. 108 | ``` 109 | 110 | Example: 111 | 112 | ``` 113 | {exp:streeng find="http:\/\/(.*).com" replace="https://www.caddis.co"} 114 | https://caddisint.com 115 | {/exp:streeng} 116 | ``` 117 | 118 | Result: 119 | 120 | ``` 121 | https://www.caddis.co 122 | ``` 123 | 124 | #### Special Characters in Find and Replace 125 | 126 | Streng also provides some keywords to search for special characters. 127 | 128 | **`find="NEWLINE"`** — Finds a new line 129 | **`find="PIPE"`** — Find the | character 130 | **`find="QUOTE`** — Find a prime quote: " 131 | **`find="SPACE"`** — Find a space character 132 | 133 | ### Trim 134 | 135 | ``` 136 | trim="left" 137 | ``` 138 | 139 | Specify the trimming of white space with this parameter. Default = "both" 140 | 141 | ### Encoding 142 | 143 | ``` 144 | encode="yes" 145 | decode="yes" 146 | specialchars="yes" 147 | url="yes" 148 | ``` 149 | 150 | HTML encode or decode, or URL encode a string. 151 | 152 | #### HTML Encode 153 | 154 | With `encode="yes"`, a prime quotation mark would be encoded as `"` and ampersands encoded as `&` 155 | 156 | #### Additional encode/decode parameters 157 | 158 | When using `encode` you can also use `encode_flags` and `encode_encoding` parameters. 159 | http://php.net/manual/en/function.html-entity-decode.php 160 | 161 | When using `decode` you can also use `decode_flags` and `decode_encoding` parameters. 162 | http://php.net/manual/en/function.htmlentities.php 163 | 164 | When using `specialchars` you can also use `specialchars_flags` and `specialchars_encoding` parameters. 165 | http://php.net/manual/en/function.htmlspecialchars.php 166 | 167 | Example: 168 | 169 | ``` 170 | {exp:streeng encode="yes"} 171 | "I'm a test" string & boy don't I look cool? Looking cool really helps with world domination! 172 | {/exp:streeng} 173 | ``` 174 | 175 | Result: 176 | 177 | ``` 178 | "I'm a test" string & boy don't I look cool? Looking cool really helps with world domination! 179 | ``` 180 | 181 | This is really useful for protecting code examples and the like. 182 | 183 | Example: 184 | 185 | ``` 186 | {exp:streeng encode="yes"} 187 | Streeng will encode prime quotation marks as " entities. 188 | {/exp:streeng} 189 | ``` 190 | 191 | Result: 192 | 193 | ``` 194 | Streeng will encode prime quotation marks as " entities. 195 | ``` 196 | 197 | Or you can reverse the process with decode="yes". 198 | 199 | Example: 200 | 201 | ``` 202 | {exp:streeng decode="yes"} 203 | Streeng will encode prime quotation marks as " entities. 204 | {/exp:streeng} 205 | ``` 206 | 207 | Result: 208 | 209 | ``` 210 | Streeng will encode prime quotation marks as " entities. 211 | ``` 212 | 213 | #### URL Encode 214 | 215 | If you need to url encode a string, use `url="yes"`. Say you need to encode segments for use in a querystring. Like `/account/login?return=my/url/segments`. Of course those slashes in the querystring would need to be encoded. 216 | 217 | Example: 218 | 219 | ``` 220 | {exp:streeng url="yes"} 221 | my/url/segments 222 | {/exp:streeng} 223 | ``` 224 | 225 | Result: 226 | 227 | ``` 228 | my%2Furl%2Fsegments 229 | ``` 230 | 231 | ### Sentence Manipulations 232 | 233 | **`capitalize="yes"`** — capitalize the first character of the string 234 | **`title="yes"`** — capitalize the first character of every word 235 | **`lower="yes"`** — convert the string to lower case 236 | **`upper="yes"`** – convert the string to upper case 237 | 238 | ### Limiting and Truncating 239 | 240 | All limiting and truncating will only count characters and words that are not HTML tags, and HTML tags will be auto-closed for you. In addition, character truncating is word aware and will not cut off words in the middle. Instead, Streeng will truncating immediately preceding the word that would otherwise be truncated. 241 | 242 | Here are the parameters: 243 | 244 | **`characters="20"`** — truncate the string by character count 245 | **`words="10"`** — truncate the string by word count 246 | **`append="&hellip"`** — if truncated this will be appended to the end of the string 247 | 248 | ### Slug 249 | 250 | This function will replace spaces in your string with dashes (default) or whatever you specify. 251 | 252 | **`slug="yes"`** — Replace spaces in the string with dashes, or your specified separator 253 | **`separator="_"`** — Optionally specify the separator to use in the slug 254 | 255 | ### Repeat 256 | 257 | **`repeat="3"`** — Specify the number of times to repeat the string between the tag pair. This is in addition to the initial content. So if you specify the number 2, you will see your string three times. 258 | 259 | ### Counts and Splits 260 | 261 | **`count="|"`** — return the number of instances the supplied string appears. Pass "ALL" to count all characters. 262 | **`splits="|"`** — return the number of exploded values from the supplied string 263 | 264 | ### Typography 265 | 266 | Use the `typography` parameter to parse a string through ExpressionEngine's typography parser. Valid options are: 267 | 268 | - `all` 269 | - `xhtml` 270 | - `br` 271 | - `lite` 272 | 273 | Example: 274 | 275 | ``` 276 | {exp:streeng typography="lite"} 277 | "I'm a test" string & boy don't I look cool? Looking cool really helps with world domination! 278 | {/exp:streeng} 279 | ``` 280 | 281 | Result: 282 | 283 | ``` 284 | “I’m a test” string & boy don’t I look cool? Looking cool really helps with world domination! 285 | ``` 286 | 287 | Example: 288 | 289 | ``` 290 | {exp:streeng typography="all"} 291 | "I'm a test" string & boy don't I look cool? Looking cool really helps with world domination! 292 | 293 | Paragraphs also are a good thing to have for world domination! 294 | {/exp:streeng} 295 | ``` 296 | 297 | Result: 298 | 299 | ``` 300 |“I’m a test” string & boy don’t I look cool? Looking cool really helps with world domination!
301 | 302 |Paragraphs also are a good thing to have for world domination!
303 | ``` 304 | 305 | ## Installation 306 | 307 | ### EE 2 308 | 309 | DevDemon Updater is fully supported, or for manual installs copy "system/expressionengine/third_party/streeng" to your third_party system directory. 310 | 311 | ### EE 3 312 | 313 | 1. Copy "system/expressionengine/third_party/streeng" to "system/user/addons" 314 | 2. Go to your control panel and navigate to the Add-On Manager 315 | 3. Locate Streeng in the Third Party Add-Ons section and click install 316 | 317 | 318 | ## License 319 | 320 | Copyright 2016 [Caddis Interactive, LLC](https://www.caddis.co). Licensed under the [Apache License, Version 2.0](https://github.com/caddis/streeng/blob/master/LICENSE). -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /system/expressionengine/third_party/streeng/pi.streeng.php: -------------------------------------------------------------------------------- 1 | STREENG_AUTHOR, 7 | 'pi_author_url' => STREENG_AUTHOR_URL, 8 | 'pi_description' => STREENG_DESC, 9 | 'pi_name' => STREENG_NAME, 10 | 'pi_version' => STREENG_VER, 11 | 'pi_usage' => Streeng::usage() 12 | ); 13 | 14 | class Streeng 15 | { 16 | public $return_data = ''; 17 | 18 | public function __construct() { 19 | $string = ee()->TMPL->tagdata; 20 | 21 | // Strip tags 22 | $allowed = ee()->TMPL->fetch_param('allowed'); 23 | 24 | if ($allowed !== false) { 25 | $tags = explode('|', $allowed); 26 | $allow = '<' . implode('>,<', $tags) . '>'; 27 | 28 | $string = ($allowed === 'none') ? 29 | strip_tags($string) : 30 | strip_tags($string, $allow); 31 | } 32 | 33 | // Find and replace 34 | $find = ee()->TMPL->fetch_param('find'); 35 | 36 | if ($find !== false) { 37 | $replace = ee()->TMPL->fetch_param('replace'); 38 | $insensitive = ee()->TMPL->fetch_param('insensitive'); 39 | 40 | if ($replace !== false) { 41 | // Options 42 | $flags = ee()->TMPL->fetch_param('flags'); 43 | 44 | // Search options 45 | $searchOptions = array( 46 | 'NEWLINE' => "\n", 47 | 'PIPE' => '\|', 48 | 'QUOTE' => '"', 49 | 'SPACE' => ' ' 50 | ); 51 | 52 | // Replacement options 53 | $replacementOptions = array( 54 | 'NEWLINE' => "\n", 55 | 'PIPE' => '|', 56 | 'QUOTE' => '"', 57 | 'SPACE' => ' ' 58 | ); 59 | 60 | $explode = ee()->TMPL->fetch_param('explode', '|'); 61 | 62 | $find = explode($explode, $find); 63 | $replace = explode($explode, $replace); 64 | 65 | foreach ($find as $i => $search) { 66 | $search = isset($searchOptions[$search]) ? 67 | $searchOptions[$search] : 68 | $search; 69 | $search = $this->_prep_regex($search, $insensitive, $flags); 70 | 71 | $replacement = isset($replace[$i]) ? 72 | $replace[$i] : 73 | $replace[0]; 74 | $replacement = isset($replacementOptions[$replacement]) ? 75 | $replacementOptions[$replacement] : 76 | $replacement; 77 | 78 | $string = preg_replace($search, $replacement, $string); 79 | } 80 | } else { 81 | $this->return_data = (( 82 | $insensitive ? 83 | stripos($string, $find) : 84 | strpos($string, $find) 85 | ) !== false) ? 1 : 0; 86 | 87 | return; 88 | } 89 | } 90 | 91 | // Trim whitespace 92 | $trim = ee()->TMPL->fetch_param('trim', 'both'); 93 | 94 | if ($trim !== 'no') { 95 | switch ($trim) { 96 | case 'both': 97 | $string = trim($string); 98 | break; 99 | case 'left': 100 | $string = ltrim($string); 101 | break; 102 | default: 103 | $string = rtrim($string); 104 | } 105 | } 106 | 107 | // URL encode 108 | $url = ee()->TMPL->fetch_param('url'); 109 | 110 | if ($url === 'yes') { 111 | $string = urlencode($string); 112 | } elseif ($url === 'raw') { 113 | $string = rawurlencode($string); 114 | } 115 | 116 | // HTML encode 117 | $encode = ee()->TMPL->fetch_param('encode'); 118 | 119 | if ($encode === 'yes') { 120 | $encode_flags = constant( 121 | ee()->TMPL->fetch_param('encode_flags', 'ENT_COMPAT') 122 | ); 123 | 124 | $encode_encoding = ee()->TMPL->fetch_param( 125 | 'encode_encoding', 126 | 'UTF-8' 127 | ); 128 | 129 | $string = htmlentities($string, $encode_flags, $encode_encoding); 130 | } 131 | 132 | // HTML decode 133 | $decode = ee()->TMPL->fetch_param('decode'); 134 | 135 | if ($decode === 'yes') { 136 | $decode_flags = constant( 137 | ee()->TMPL->fetch_param('decode_flags', 'ENT_COMPAT') 138 | ); 139 | 140 | $decode_encoding = ee()->TMPL->fetch_param('decode_flags', 'UTF-8'); 141 | 142 | $string = html_entity_decode( 143 | $string, 144 | $decode_flags, 145 | $decode_encoding 146 | ); 147 | } 148 | 149 | // HTML special characters 150 | $specialchars = ee()->TMPL->fetch_param('specialchars'); 151 | 152 | if ($specialchars === 'yes') { 153 | $specialchars_flags = constant( 154 | ee()->TMPL->fetch_param('specialchars_flags', 'ENT_COMPAT') 155 | ); 156 | 157 | $specialchars_encoding = ee()->TMPL->fetch_param( 158 | 'decode_flags', 159 | 'UTF-8' 160 | ); 161 | 162 | $string = htmlspecialchars( 163 | $string, 164 | $specialchars_flags, 165 | $specialchars_encoding 166 | ); 167 | } 168 | 169 | // Capitalize 170 | $capitalize = ee()->TMPL->fetch_param('capitalize'); 171 | 172 | if ($capitalize === 'yes') { 173 | $string = ucfirst($string); 174 | } 175 | 176 | // Title case 177 | $title = ee()->TMPL->fetch_param('title'); 178 | 179 | if ($title === 'yes') { 180 | $string = ucwords(strtolower($string)); 181 | } 182 | 183 | // Lowercase 184 | $lower = ee()->TMPL->fetch_param('lower'); 185 | 186 | if ($lower === 'yes') { 187 | $string = strtolower($string); 188 | } 189 | 190 | // Uppercase 191 | $upper = ee()->TMPL->fetch_param('upper'); 192 | 193 | if ($upper === 'yes') { 194 | $string = strtoupper($string); 195 | } 196 | 197 | // Truncate 198 | $characters = (int) ee()->TMPL->fetch_param('characters'); 199 | $words = (int) ee()->TMPL->fetch_param('words'); 200 | $append = ee()->TMPL->fetch_param('append', '…'); 201 | 202 | if ($words !== 0) { 203 | $temp_string = strip_tags($string); 204 | $temp_string = explode(' ', $temp_string); 205 | $temp_string = implode(' ', array_splice($temp_string, 0, $words + 1)); 206 | 207 | if ($allowed === 'none') { 208 | $string = strlen($temp_string) < strlen($string) ? 209 | ($temp_string . $append) : 210 | $temp_string; 211 | } else { 212 | $characters = strlen($temp_string); 213 | $string = $this->_truncate_markup($string, $characters, $append, true, true); 214 | } 215 | } elseif ($characters !== 0) { 216 | if ($allowed === 'none') { 217 | $temp_string = strip_tags($string); 218 | 219 | if (strlen($temp_string) > $characters) { 220 | $pos = strrpos(substr($temp_string, 0, $characters), ' '); 221 | $string = substr($temp_string, 0, $pos) . $append; 222 | } 223 | } else { 224 | $string = $this->_truncate_markup( 225 | $string, $characters, $append, true, true 226 | ); 227 | } 228 | } 229 | 230 | // Slug 231 | $slug = ee()->TMPL->fetch_param('slug'); 232 | 233 | if ($slug === 'yes') { 234 | $separator = ee()->TMPL->fetch_param('separator', '-'); 235 | 236 | $string = preg_replace('/[^A-Za-z0-9-]+/', $separator, $string); 237 | } 238 | 239 | // Parse typography 240 | $typography = ee()->TMPL->fetch_param('typography'); 241 | 242 | if ($typography !== false) { 243 | $typographyAllowed = array( 244 | 'all', 245 | 'xhtml', 246 | 'br', 247 | 'lite' 248 | ); 249 | 250 | if (in_array($typography, $typographyAllowed)) { 251 | ee()->load->library('typography'); 252 | 253 | ee()->typography->initialize(); 254 | 255 | $typographyPrefs = array( 256 | 'text_format' => $typography, 257 | 'html_format' => 'all', 258 | 'auto_links' => 'n', 259 | 'allow_img_url' => 'y' 260 | ); 261 | 262 | $string = ee()->typography->parse_type( 263 | $string, 264 | $typographyPrefs 265 | ); 266 | } 267 | } 268 | 269 | // Repeat 270 | $repeat = (int) ee()->TMPL->fetch_param('repeat', 0); 271 | 272 | if ($repeat > 0) { 273 | $string .= str_repeat($string, $repeat); 274 | } 275 | 276 | // Substring count 277 | $count = ee()->TMPL->fetch_param('count'); 278 | 279 | if ($count !== false) { 280 | if ($count === 'ALL') { 281 | $string = strlen($string); 282 | } else { 283 | $string = substr_count($string, $count); 284 | } 285 | } 286 | 287 | // Split count 288 | $splits = ee()->TMPL->fetch_param('splits'); 289 | 290 | if ($splits !== false) { 291 | $string = count(explode($splits, $string)); 292 | } 293 | 294 | $this->return_data = $string; 295 | } 296 | 297 | private function _prep_regex($string, $insensitive = true, $flags = false) 298 | { 299 | // Check containing characters 300 | if (substr($string, 0, 1) !== '/' || substr($string, 0, 2) === '\/') { 301 | $string = '/' . $string; 302 | } 303 | 304 | if (substr($string, -1, 1) !== '/' || substr($string, -2, 2) === '\/') { 305 | $string .= '/'; 306 | } 307 | 308 | // Pattern modifiers 309 | if ($flags) { 310 | $string .= str_replace('i', '', $flags); 311 | } 312 | 313 | if (! $insensitive) { 314 | $string .= 'i'; 315 | } 316 | 317 | return $string; 318 | } 319 | 320 | /** 321 | * @package php-shorten 322 | * @example example.html.php 323 | * @link https://github.com/Dreamseer/php-shorten/ 324 | * @author Marc Görtz (http://marcgoertz.de/) 325 | * @license MIT License 326 | * @copyright Copyright (c) 2011-2013, Marc Görtz 327 | * @version 1.1.0 328 | */ 329 | 330 | private function _truncate_markup($markup, $length = 400, $appendix = '…', $appendixInside = false, $wordsafe = false) { 331 | $truncated = ''; 332 | $lengthOutput = 0; 333 | $position = 0; 334 | $tags = array(); 335 | 336 | // To avoid UTF-8 multibyte glitches we need entities, but no special characters for tags or existing entities 337 | $markup = str_replace(array( 338 | '<', '>', '&', 339 | ), array( 340 | '<', '>', '&', 341 | ), htmlentities($markup, ENT_NOQUOTES, 'UTF-8')); 342 | 343 | // Loop through text 344 | while ($lengthOutput < $length && preg_match('{?([a-z]+)[^>]*>|?[a-zA-Z0-9]+;}', $markup, $match, PREG_OFFSET_CAPTURE, $position)) { 345 | list($tag, $positionTag) = $match[0]; 346 | 347 | // Add text leading up to the tag or entity 348 | $text = substr($markup, $position, $positionTag - $position); 349 | 350 | if ($lengthOutput + strlen($text) > $length) { 351 | $truncated .= substr($text, 0, $length - $lengthOutput); 352 | $lengthOutput = $length; 353 | 354 | break; 355 | } 356 | 357 | $truncated .= $text; 358 | $lengthOutput += strlen($text); 359 | 360 | // Add tags and entities 361 | if ($tag[0] === '&') { 362 | // Handle the entity 363 | $truncated .= $tag; 364 | // Which is only one character 365 | $lengthOutput++; 366 | } else { 367 | // Handle the tag 368 | $tagName = $match[1][0]; 369 | 370 | if ($tag[1] === '/') { 371 | // This is a closing tag 372 | $openingTag = array_pop($tags); 373 | // Check that tags are properly nested 374 | assert($openingTag === $tagName); 375 | $truncated .= $tag; 376 | } elseif ($tag[strlen($tag) - 2] === '/') { 377 | // Self-closing tag in XML dialect 378 | $truncated .= $tag; 379 | } else { 380 | // Opening tag 381 | $truncated .= $tag; 382 | $tags[] = $tagName; 383 | } 384 | } 385 | 386 | // Continue after the tag 387 | $position = $positionTag + strlen($tag); 388 | } 389 | 390 | // Add any remaining text 391 | if ($lengthOutput < $length && $position < strlen($markup)) { 392 | $truncated .= substr($markup, $position, $length - $lengthOutput); 393 | } 394 | 395 | if (strlen($truncated) < strlen($markup)) { 396 | // If the words shouldn't be cut in the middle 397 | if ($wordsafe) { 398 | // Search the last occurrence of a space 399 | $spacepos = strrpos($truncated, ' '); 400 | 401 | if ($spacepos !== false) { 402 | // Cut the text in this position 403 | $truncated = substr($truncated, 0, $spacepos); 404 | } 405 | } 406 | 407 | // Add appendix to last tag content 408 | if ($appendixInside) { 409 | $truncated .= $appendix; 410 | } 411 | 412 | // Close any open tags 413 | while (! empty($tags)) { 414 | $truncated .= sprintf('%s>', array_pop($tags)); 415 | } 416 | 417 | return ($appendixInside) ? $truncated : $truncated . $appendix; 418 | } 419 | 420 | return $truncated; 421 | } 422 | 423 | public static function usage() { 424 | return 'See docs and examples at https://github.com/caddis/streeng'; 425 | } 426 | } 427 | --------------------------------------------------------------------------------