There are a few short-code libraries out there already for Codeigniter but for the project I am currently working on I needed a bit more control. Here is my solution for implementing a short-codes system similar to the one built into WordPress.
Each short-code is surrounded by double square brackets ( [[ … ]] ), with the content of the short-code consisting of a key and a value broken up by a double colon ( :: ). An example of how an internal link might be implemented is:
[[internal::contact us]]
The library does not try to do anything clever, it just maps over a string the references to all short-codes, their key / values pairs, lengths, start and ends. There are utility functions that allow specific short-codes to be replaces with HTML snippets.
This is an example of how to incorporate the code into a Codeigniter project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$this->load->library('Shortcodes'); $this->shortcodes->process_string("jhghj jklklj [[here::wewe]] kjhjkh kjhkjhjkhjh [[http://www.-\!ghj&=@+ghjg]] kljljkjh [[jk&hjkh ]]kjhkj[[kjhjk::jkhkj]] hjkghjg"); $keys = array('internal', 'external'); $links = $this->shortcodes->return_shortcodes_by_key($keys); //echo $internal_links[0]['shortcode']->getValue(); foreach($links as $link) { $this->shortcodes->replaceShortCodeWithHTML($link['index'], '<a href="' . $link['shortcode']->getValue() . '">' . $link['shortcode']->getValue() . '</a>'); } echo $this->shortcodes->getAdaptedString(); |
Here is the library code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * Shortcodes class * * @package SwarmTv * @subpackage Libraries * @category shortcodes * @author Alcwyn Parker * @link http://www.alcwynparker.co.uk */ class Shortcodes{ private $shortcodes = null; //private $accepted_keys = array('internal', 'external', 'youtube', 'vimeo'); //private $accepted_styling = array('b','i','u'); private $original_string = ""; private $adapted_string = ""; private $modified = false; /** * Shortcodes Constructor * * Instantiate the array to store shortcodes in * */ function __construct() { $this->shortcodes = array(); } // -------------------------------------------------------------------- // PUBLIC METHODS // -------------------------------------------------------------------- /** * Break up the raw string into shortcodes and save them to list * * @access public * @return null */ public function process_string($string) { $this->original_string = $string; $this->adapted_string = $string; $pattern = "/(?<=\[\[)[\w \!\?\&\+'=@:\/\.\\\-]+(?=\]\])/"; // [[ ... ]] regex $short_code_info = null; // find all the links in $string // execute the regex on string and populate $links array storing the offset with the match preg_match_all ( $pattern, $string, $short_code_info, PREG_OFFSET_CAPTURE ); // loop through the results of the regex and process shortcodes foreach($short_code_info[0] as $info) { $sc = new Shortcode($info[0], $info[1]); $this->add_short_code($sc); } } // -------------------------------------------------------------------- /** * returns the total number of short codes in the string * * @access public * @return int */ public function length() { return (sizeof($this->shortcodes)); } // -------------------------------------------------------------------- /** * returns an array of shortcodes by their key type * @param array of string for accepted keys * @access public * @return array of arrays [shortcodes][index in main array] */ public function return_shortcodes_by_key($types) { $short_codes_by_type = array(); foreach ($types as $type) { for($i = 0; $i < sizeof($this->shortcodes);$i++) { if ($this->shortcodes[$i]->getKey() === $type) { $match['shortcode'] = $this->shortcodes[$i]; $match['index'] = $i; array_push($short_codes_by_type, $match); } } } return ($short_codes_by_type); } // -------------------------------------------------------------------- /** * alters a duplicate of the original text with the replacement for the short code * @param index of short code in the shortcode array, the replacement html * @access public * @return n/a */ public function replaceShortCodeWithHTML($index, $html) { $length = strlen($html); $original_length = $this->shortcodes[$index]->getLength(); // get the difference in the length of the short code to the new html $diff = $length - ($original_length); //loop through all the short codes and update their start and end position for ($i = 0; $i < sizeof($this->shortcodes); $i++) { if(($i != $index) && ($this->shortcodes[$i]->getStart() >$this->shortcodes[$index]->getStart())) { $this->shortcodes[$i]->moveBy($diff); } } // update the adapted string with the change $this->adapted_string = substr_replace($this->adapted_string, $html, $this->shortcodes[$index]->getStart()-2, $this->shortcodes[$index]->getLength()+4); } // -------------------------------------------------------------------- // PRIVATE METHODS // -------------------------------------------------------------------- /** * Fetch the current session data if it exists * * @access private * @return null */ private function add_short_code($short_code) { array_push($this->shortcodes, $short_code); } // -------------------------------------------------------------------- // GETTERS & SETTERS // -------------------------------------------------------------------- public function getAdaptedString() { return $this->adapted_string; } } /** * Shortcode class * * @package SwarmTv * @subpackage Libraries * @category shortcodes * @author Alcwyn Parker * @link http://www.alcwynparker.co.uk */ class Shortcode { // property declaration private $raw = null; private $length = null; private $start = 0; private $end = 0; private $key = ''; private $value = ''; /** * Shortcodes Constructor * * Initiates the starting parameters and kicks of the process of breaking * the shortcode into its constituents. */ function __construct($raw, $start ) { $this->raw = $raw; $this->start = $start; $this->process_raw_shortcode(); } // -------------------------------------------------------------------- /** * moves the start and end of a shortcode in conjunction with a shortcode modification * before it * @param the amount the code has to move by * @access public * */ public function moveBy($diff) { $this->start = $this->start + $diff; $this->end = $this->end + $diff; } // -------------------------------------------------------------------- // PRIVATE METHODS // -------------------------------------------------------------------- /** * find out more info about the shortcode and break it apart * * @access private * @return null */ private function process_raw_shortcode() { // GENERAL DETAILS $this->length = strlen($this->raw); $this->end = $this->start + $this->length; // KEY & VALUE $key_val_pair = explode('::', $this->raw); // check to see if both key and value have been given if (sizeof($key_val_pair) > 1) { $this->key = $key_val_pair[0]; $this->value = $key_val_pair[1]; }else{ $http = strpos($this->raw, 'http'); if($http === false){ $this->key = 'internal'; }else{ $this->key = 'external'; } $this->value = $this->raw; } } // -------------------------------------------------------------------- // GETTERS & SETTERS // -------------------------------------------------------------------- public function getKey() { return $this->key; } public function getValue() { return $this->value; } public function getStart() { return $this->start; } public function getLength() { return $this->length; } } |