PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/functions/funcs.strings.php

https://gitlab.com/fork/hotarucms
PHP | 607 lines | 494 code | 25 blank | 88 comment | 18 complexity | b7c0f94e7cc3bacc0324da24caa4b0c8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * A collection of functions for manipulating strings
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
  18. *
  19. * @category Content Management System
  20. * @package HotaruCMS
  21. * @author Hotaru CMS Team
  22. * @copyright Copyright (c) 2009 - 2013, Hotaru CMS
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  24. * @link http://www.hotarucms.org/
  25. */
  26. // We need to set the default internal encoding for the functions to operate properly.
  27. mb_internal_encoding("UTF-8");
  28. /**
  29. * Truncate a string
  30. *
  31. * @param string $string
  32. * @param int truncate to X characters
  33. * @para, bool $dot adds ... if true
  34. * @return string
  35. */
  36. function truncate($string, $chars=0, $dot=true)
  37. {
  38. $length = mb_strlen($string);
  39. $truncated = mb_substr(strip_tags($string), 0, $chars); // strips tags to prevent broken tags
  40. if( $dot && ($length >= $chars) ) {
  41. $truncated .= '...';
  42. }
  43. return $truncated;
  44. }
  45. /**
  46. * Strip a string from the end of a string
  47. *
  48. * @param string $string
  49. * @param string $remove part of the string to strip
  50. * @return string
  51. */
  52. function rstrtrim($str, $remove=null)
  53. {
  54. $str = (string) $str;
  55. $remove = (string) $remove;
  56. if( empty($remove) ) {
  57. return rtrim($str);
  58. }
  59. $len = mb_strlen($remove);
  60. $offset = mb_strlen($str) - $len;
  61. while( $offset > 0 && $offset == mb_strpos($str, $remove, $offset) ) {
  62. $str = mb_substr($str, 0, $offset);
  63. $offset = mb_strlen($str) - $len;
  64. }
  65. return rtrim($str);
  66. }
  67. /**
  68. * Changes 'plugin_name' into 'Plugin Name'
  69. *
  70. * @param string $string e.g. a plugin folder name
  71. * @param string $delim - the character to replace underscores with
  72. * @return string
  73. */
  74. function make_name($string, $delim = '_', $caps = true)
  75. {
  76. $dep_array = array( );
  77. $dep_array = explode($delim, trim($string));
  78. if( $caps ) {
  79. $dep_array = array_map('ucfirst', $dep_array);
  80. $string = implode(' ', $dep_array);
  81. } else {
  82. $string = ucfirst(implode(' ', $dep_array));
  83. }
  84. return $string;
  85. }
  86. /**
  87. * Generates a random string
  88. *
  89. * @param int $length
  90. * @return string
  91. * @link http://us2.php.net/manual/en/ref.strings.php (Moe 10-July-2007)
  92. */
  93. function random_string($length = 8)
  94. {
  95. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxwz0123456789";
  96. $string = '';
  97. for( $i = 0; $i < $length; $i++ ) {
  98. $rand_key = mt_rand(0, strlen($chars));
  99. $string .= substr($chars, $rand_key, 1);
  100. }
  101. return str_shuffle($string);
  102. }
  103. /**
  104. * Sanitize input
  105. *
  106. * @param string $var the string to sanitize
  107. * @param string $santype type of sanitation: 'all', 'ents', 'tags'
  108. * @param string $allowable_tags
  109. * @return string|false
  110. *
  111. * Note: Borrowed from SWCMS
  112. */
  113. function sanitize($var, $santype = 'all', $allowable_tags = '')
  114. {
  115. // htmlentities & Strip tags
  116. if( $santype == 'all' ) {
  117. if( !get_magic_quotes_gpc() ) {
  118. return htmlentities(strip_tags($var, $allowable_tags), ENT_QUOTES, 'UTF-8');
  119. }
  120. return stripslashes(htmlentities(strip_tags($var, $allowable_tags), ENT_QUOTES, 'UTF-8'));
  121. }
  122. // Strip tags
  123. if( $santype == 'tags' ) {
  124. if( !get_magic_quotes_gpc() ) {
  125. return strip_tags($var, $allowable_tags);
  126. }
  127. return stripslashes(strip_tags($var, $allowable_tags));
  128. }
  129. // htmlentities
  130. if( $santype == 'ents' ) {
  131. if( !get_magic_quotes_gpc() ) {
  132. return htmlentities($var, ENT_QUOTES, 'UTF-8');
  133. }
  134. return stripslashes(htmlentities($var, ENT_QUOTES, 'UTF-8'));
  135. }
  136. return false;
  137. }
  138. /**
  139. * Make a url friendly - a dash-separated string
  140. *
  141. * @param string $input url to format
  142. * @return string|false
  143. *
  144. * Note: These functions seem to overlap each other a bit...
  145. */
  146. function make_url_friendly($input)
  147. {
  148. $output = replace_symbols($input);
  149. $output = mb_substr($output, 0, 240);
  150. $output = mb_strtolower($output, 'UTF-8');
  151. $output = trim($output);
  152. //From Wordpress and http://www.bernzilla.com/item.php?id=1007
  153. $output = sanitize_title_with_dashes($output);
  154. $output = urldecode($output);
  155. if( $output ) {
  156. return $output;
  157. }
  158. return false;
  159. }
  160. /**
  161. * Replace symbols and ascii characters with simpler alternatives
  162. *
  163. * @param string $input
  164. * @return string
  165. *
  166. * Note: Adapted from SWCMS
  167. */
  168. function replace_symbols($input)
  169. {
  170. // FOR THIS TO WORK, THIS FUNCS.STRINGS.PHP FILE MUST BE SAVED
  171. // IN UTF-8 CHARACTER ENCODING !!!
  172. // Replace spaces with hyphens
  173. $output = preg_replace('/\s+/', '-', $input);
  174. // Replace other characters
  175. $output = str_replace("--", "-", $output);
  176. $output = str_replace("/", "", $output);
  177. $output = str_replace("\\", "", $output);
  178. $output = str_replace("'", "", $output);
  179. $output = str_replace(",", "", $output);
  180. $output = str_replace(";", "", $output);
  181. $output = str_replace(":", "", $output);
  182. $output = str_replace(".", "-", $output);
  183. $output = str_replace("?", "", $output);
  184. $output = str_replace("=", "-", $output);
  185. $output = str_replace("+", "", $output);
  186. $output = str_replace("$", "", $output);
  187. $output = str_replace("&", "", $output);
  188. $output = str_replace("!", "", $output);
  189. $output = str_replace(">>", "-", $output);
  190. $output = str_replace(">", "-", $output);
  191. $output = str_replace("<<", "-", $output);
  192. $output = str_replace("<", "-", $output);
  193. $output = str_replace("*", "", $output);
  194. $output = str_replace(")", "", $output);
  195. $output = str_replace("(", "", $output);
  196. $output = str_replace("[", "", $output);
  197. $output = str_replace("]", "", $output);
  198. $output = str_replace("^", "", $output);
  199. $output = str_replace("%", "", $output);
  200. $output = str_replace("#", "", $output);
  201. $output = str_replace("@", "", $output);
  202. $output = str_replace("`", "", $output);
  203. $output = str_replace("‘", "", $output);
  204. $output = str_replace("’", "", $output);
  205. $output = str_replace("“", "", $output);
  206. $output = str_replace("”", "", $output);
  207. $output = str_replace("~", "", $output);
  208. $output = str_replace("–", "-", $output);
  209. $output = str_replace("\"", "", $output);
  210. $output = str_replace("|", "", $output);
  211. $output = str_replace("«", "", $output);
  212. $output = str_replace("»", "", $output);
  213. $output = str_replace("‹", "", $output);
  214. $output = str_replace("›", "", $output);
  215. $output = str_replace("…", "", $output);
  216. $output = str_replace("--", "-", $output);
  217. $output = str_replace("---", "-", $output);
  218. $output = str_replace("—", "-", $output);
  219. return $output;
  220. }
  221. /**
  222. * Get rid of any dangerous or unwanted characters
  223. *
  224. * @param string $title
  225. *
  226. * Note: Borrowed from Wordpress
  227. */
  228. function sanitize_title_with_dashes($title)
  229. {
  230. $title = strip_tags($title);
  231. // Preserve escaped octets.
  232. $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
  233. // Remove percent signs that are not part of an octet.
  234. $title = str_replace('%', '', $title);
  235. // Restore octets.
  236. $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
  237. $title = remove_accents($title);
  238. $title = mb_strtolower($title, 'UTF-8');
  239. if( seems_utf8($title) ) {
  240. $title = utf8_uri_encode($title, 200);
  241. }
  242. $title = preg_replace('/&.+?;/', '', $title); // kill entities
  243. $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
  244. $title = preg_replace('/\s+/', '-', $title);
  245. $title = preg_replace('|-+|', '-', $title);
  246. $title = trim($title, '-');
  247. return $title;
  248. }
  249. /**
  250. * Remove accents from characters
  251. *
  252. * @param string $string
  253. * @return string
  254. *
  255. * Note: Borrowed from Wordpress
  256. */
  257. function remove_accents($string)
  258. {
  259. if( !preg_match('/[\x80-\xff]/', $string) ) {
  260. return $string;
  261. }
  262. if( seems_utf8($string) ) {
  263. $chars = array(
  264. // Decompositions for Latin-1 Supplement
  265. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  266. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  267. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  268. chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  269. chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  270. chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  271. chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  272. chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  273. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  274. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  275. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  276. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  277. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  278. chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  279. chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  280. chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  281. chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  282. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  283. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  284. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  285. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  286. chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  287. chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  288. chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  289. chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  290. chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  291. chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  292. chr(195).chr(191) => 'y',
  293. // Decompositions for Latin Extended-A
  294. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  295. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  296. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  297. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  298. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  299. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  300. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  301. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  302. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  303. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  304. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  305. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  306. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  307. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  308. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  309. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  310. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  311. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  312. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  313. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  314. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  315. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  316. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  317. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  318. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  319. chr(196).chr(178) => 'IJ', chr(196).chr(179) => 'ij',
  320. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  321. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  322. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  323. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  324. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  325. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  326. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  327. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  328. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  329. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  330. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  331. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  332. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  333. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  334. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  335. chr(197).chr(146) => 'OE', chr(197).chr(147) => 'oe',
  336. chr(197).chr(148) => 'R', chr(197).chr(149) => 'r',
  337. chr(197).chr(150) => 'R', chr(197).chr(151) => 'r',
  338. chr(197).chr(152) => 'R', chr(197).chr(153) => 'r',
  339. chr(197).chr(154) => 'S', chr(197).chr(155) => 's',
  340. chr(197).chr(156) => 'S', chr(197).chr(157) => 's',
  341. chr(197).chr(158) => 'S', chr(197).chr(159) => 's',
  342. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  343. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  344. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  345. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  346. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  347. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  348. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  349. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  350. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  351. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  352. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  353. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  354. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  355. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  356. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  357. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  358. // Euro Sign
  359. chr(226).chr(130).chr(172) => 'E',
  360. // GBP (Pound) Sign
  361. chr(194).chr(163) => '' );
  362. $string = strtr($string, $chars);
  363. }
  364. else {
  365. // Assume ISO-8859-1 if not UTF-8
  366. $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
  367. .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
  368. .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
  369. .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
  370. .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
  371. .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
  372. .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
  373. .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
  374. .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
  375. .chr(252).chr(253).chr(255);
  376. $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
  377. $string = strtr($string, $chars['in'], $chars['out']);
  378. $double_chars['in'] = array( chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254) );
  379. $double_chars['out'] = array( 'OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th' );
  380. $string = str_replace($double_chars['in'], $double_chars['out'], $string);
  381. }
  382. return $string;
  383. }
  384. /**
  385. * Determine if the string is utf8
  386. *
  387. * @param string $str
  388. * @return bool
  389. *
  390. * Note: Borrowed from Wordpress (by bmorel at ssi dot fr )
  391. */
  392. function seems_utf8($str)
  393. {
  394. $length = strlen($str);
  395. for( $i = 0; $i < $length; $i++ ) {
  396. if( ord($str[$i]) < 0x80 ) {
  397. continue; // 0bbbbbbb
  398. } elseif( (ord($str[$i]) & 0xE0) == 0xC0 ) {
  399. $n = 1; // 110bbbbb
  400. } elseif( (ord($str[$i]) & 0xF0) == 0xE0 ) {
  401. $n = 2; // 1110bbbb
  402. } elseif( (ord($str[$i]) & 0xF8) == 0xF0 ) {
  403. $n = 3; // 11110bbb
  404. } elseif( (ord($str[$i]) & 0xFC) == 0xF8 ) {
  405. $n = 4; // 111110bb
  406. } elseif( (ord($str[$i]) & 0xFE) == 0xFC ) {
  407. $n = 5; // 1111110b
  408. } else {
  409. return false; // Does not match any model
  410. }
  411. for( $j = 0; $j < $n; $j++ ) {
  412. // n bytes matching 10bbbbbb follow ?
  413. if( (++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80) ) {
  414. return false;
  415. }
  416. }
  417. }
  418. return true;
  419. }
  420. /**
  421. * Encodes a utf8 string
  422. *
  423. * @param string $utf8_string
  424. * @param int $length
  425. * @return string
  426. *
  427. * Note: Borrowed from Wordpress
  428. */
  429. function utf8_uri_encode($utf8_string, $length = 0)
  430. {
  431. $unicode = '';
  432. $values = array( );
  433. $num_octets = 1;
  434. $unicode_length = 0;
  435. $string_length = strlen($utf8_string);
  436. for( $i = 0; $i < $string_length; $i++ ) {
  437. $value = ord($utf8_string[$i]);
  438. if( $value < 128 ) {
  439. if( $length && ( $unicode_length >= $length ) ) {
  440. break;
  441. }
  442. $unicode .= chr($value);
  443. $unicode_length++;
  444. } else {
  445. if( count($values) == 0 ) {
  446. $num_octets = ( $value < 224 ) ? 2 : 3;
  447. }
  448. $values[] = $value;
  449. if( $length && ($unicode_length + ($num_octets * 3)) > $length ) {
  450. break;
  451. }
  452. if( count($values) == $num_octets ) {
  453. if( $num_octets == 3 ) {
  454. $unicode .= '%'.dechex($values[0]).'%'.dechex($values[1]).'%'.dechex($values[2]);
  455. $unicode_length += 9;
  456. } else {
  457. $unicode .= '%'.dechex($values[0]).'%'.dechex($values[1]);
  458. $unicode_length += 6;
  459. }
  460. $values = array( );
  461. $num_octets = 1;
  462. }
  463. }
  464. }
  465. return $unicode;
  466. }
  467. /**
  468. * Strip domain from url
  469. *
  470. * @param string $url
  471. * @return string|false $domain - including http://
  472. */
  473. function get_domain($url = '')
  474. {
  475. $parsed = parse_url($url);
  476. if( isset($parsed['scheme']) ) {
  477. $domain = $parsed['scheme']."://".$parsed['host'];
  478. return $domain;
  479. }
  480. return false;
  481. }
  482. if( !function_exists("iconv") ) {
  483. /**
  484. * Convert string to requested character encoding if iconv library not installed
  485. *
  486. * @param string $from
  487. * @param string $to
  488. * @param string $string
  489. * @return string
  490. * @link http://www.jpfox.fr/?post/2007/07/25/165-alternative-a-la-fonction-php-iconv
  491. */
  492. function iconv($from, $to, $string)
  493. {
  494. $converted = htmlentities($string, ENT_NOQUOTES, $from);
  495. $converted = html_entity_decode($converted, ENT_NOQUOTES, $to);
  496. return $converted;
  497. }
  498. }
  499. /**
  500. * Count urls within a block of text
  501. *
  502. * @return int
  503. * @link http://www.liamdelahunty.com/tips/php_url_count_check_for_comment_spam.php
  504. */
  505. function countUrls($text = '')
  506. {
  507. //$http = substr_count($text, "http");
  508. $href = substr_count($text, "href");
  509. $url = substr_count($text, "[url");
  510. return $href + $url;
  511. }
  512. /**
  513. * Convert textual urls into clickable HTML links, and add nofollow.
  514. *
  515. * @param string $string
  516. * @return string
  517. */
  518. function make_urls_clickable($string)
  519. {
  520. $string = trim($string);
  521. // Super RegEx for converting urls into clickable links unless already in an anchor tag.
  522. $string = preg_replace('|(?<!href=[\"\'])(https?://[A-Za-z0-9+\-=._/*(),@\'$:;&!?%]+)|i', '<a href="$1">$1</a>', $string);
  523. // Add nofollow to links
  524. $string = preg_replace('/a[ ]+href[ ]*=[ ]*"http:\/\/(.*?)".*?/i', 'a href="http://$1" rel="nofollow"', $string);
  525. return $string;
  526. }
  527. /**
  528. * Strip foreign characters from latin1/utf8 database yuckiness
  529. *
  530. * @param string $str
  531. * @return string
  532. */
  533. function strip_foreign_characters($str)
  534. {
  535. $str = str_replace('Â', '', $str);
  536. $str = str_replace('’', '\'', $str);
  537. $str = str_replace('–', '-', $str);
  538. $str = str_replace('“', '"', $str);
  539. $str = str_replace('”', '"', $str);
  540. return $str;
  541. }
  542. ?>