You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_html2text.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2008-2012, The Roundcube Dev Team |
  6. | Copyright (c) 2005-2007, Jon Abernathy <jon@chuggnutt.com> |
  7. | |
  8. | Licensed under the GNU General Public License version 3 or |
  9. | any later version with exceptions for skins & plugins. |
  10. | See the README file for a full license statement. |
  11. | |
  12. | PURPOSE: |
  13. | Converts HTML to formatted plain text (based on html2text class) |
  14. +-----------------------------------------------------------------------+
  15. | Author: Thomas Bruederli <roundcube@gmail.com> |
  16. | Author: Aleksander Machniak <alec@alec.pl> |
  17. | Author: Jon Abernathy <jon@chuggnutt.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. /**
  21. * Takes HTML and converts it to formatted, plain text.
  22. *
  23. * Thanks to Alexander Krug (http://www.krugar.de/) to pointing out and
  24. * correcting an error in the regexp search array. Fixed 7/30/03.
  25. *
  26. * Updated set_html() function's file reading mechanism, 9/25/03.
  27. *
  28. * Thanks to Joss Sanglier (http://www.dancingbear.co.uk/) for adding
  29. * several more HTML entity codes to the $search and $replace arrays.
  30. * Updated 11/7/03.
  31. *
  32. * Thanks to Darius Kasperavicius (http://www.dar.dar.lt/) for
  33. * suggesting the addition of $allowed_tags and its supporting function
  34. * (which I slightly modified). Updated 3/12/04.
  35. *
  36. * Thanks to Justin Dearing for pointing out that a replacement for the
  37. * <TH> tag was missing, and suggesting an appropriate fix.
  38. * Updated 8/25/04.
  39. *
  40. * Thanks to Mathieu Collas (http://www.myefarm.com/) for finding a
  41. * display/formatting bug in the _build_link_list() function: email
  42. * readers would show the left bracket and number ("[1") as part of the
  43. * rendered email address.
  44. * Updated 12/16/04.
  45. *
  46. * Thanks to Wojciech Bajon (http://histeria.pl/) for submitting code
  47. * to handle relative links, which I hadn't considered. I modified his
  48. * code a bit to handle normal HTTP links and MAILTO links. Also for
  49. * suggesting three additional HTML entity codes to search for.
  50. * Updated 03/02/05.
  51. *
  52. * Thanks to Jacob Chandler for pointing out another link condition
  53. * for the _build_link_list() function: "https".
  54. * Updated 04/06/05.
  55. *
  56. * Thanks to Marc Bertrand (http://www.dresdensky.com/) for
  57. * suggesting a revision to the word wrapping functionality; if you
  58. * specify a $width of 0 or less, word wrapping will be ignored.
  59. * Updated 11/02/06.
  60. *
  61. * *** Big housecleaning updates below:
  62. *
  63. * Thanks to Colin Brown (http://www.sparkdriver.co.uk/) for
  64. * suggesting the fix to handle </li> and blank lines (whitespace).
  65. * Christian Basedau (http://www.movetheweb.de/) also suggested the
  66. * blank lines fix.
  67. *
  68. * Special thanks to Marcus Bointon (http://www.synchromedia.co.uk/),
  69. * Christian Basedau, Norbert Laposa (http://ln5.co.uk/),
  70. * Bas van de Weijer, and Marijn van Butselaar
  71. * for pointing out my glaring error in the <th> handling. Marcus also
  72. * supplied a host of fixes.
  73. *
  74. * Thanks to Jeffrey Silverman (http://www.newtnotes.com/) for pointing
  75. * out that extra spaces should be compressed--a problem addressed with
  76. * Marcus Bointon's fixes but that I had not yet incorporated.
  77. *
  78. * Thanks to Daniel Schledermann (http://www.typoconsult.dk/) for
  79. * suggesting a valuable fix with <a> tag handling.
  80. *
  81. * Thanks to Wojciech Bajon (again!) for suggesting fixes and additions,
  82. * including the <a> tag handling that Daniel Schledermann pointed
  83. * out but that I had not yet incorporated. I haven't (yet)
  84. * incorporated all of Wojciech's changes, though I may at some
  85. * future time.
  86. *
  87. * *** End of the housecleaning updates. Updated 08/08/07.
  88. */
  89. /**
  90. * Converts HTML to formatted plain text
  91. *
  92. * @package Framework
  93. * @subpackage Utils
  94. */
  95. class rcube_html2text
  96. {
  97. /**
  98. * Contains the HTML content to convert.
  99. *
  100. * @var string $html
  101. */
  102. protected $html;
  103. /**
  104. * Contains the converted, formatted text.
  105. *
  106. * @var string $text
  107. */
  108. protected $text;
  109. /**
  110. * Maximum width of the formatted text, in columns.
  111. *
  112. * Set this value to 0 (or less) to ignore word wrapping
  113. * and not constrain text to a fixed-width column.
  114. *
  115. * @var integer $width
  116. */
  117. protected $width = 70;
  118. /**
  119. * Target character encoding for output text
  120. *
  121. * @var string $charset
  122. */
  123. protected $charset = 'UTF-8';
  124. /**
  125. * List of preg* regular expression patterns to search for,
  126. * used in conjunction with $replace.
  127. *
  128. * @var array $search
  129. * @see $replace
  130. */
  131. protected $search = array(
  132. '/\r/', // Non-legal carriage return
  133. '/^.*<body[^>]*>\n*/is', // Anything before <body>
  134. '/<head[^>]*>.*?<\/head>/is', // <head>
  135. '/<script[^>]*>.*?<\/script>/is', // <script>
  136. '/<style[^>]*>.*?<\/style>/is', // <style>
  137. '/[\n\t]+/', // Newlines and tabs
  138. '/<p[^>]*>/i', // <p>
  139. '/<\/p>[\s\n\t]*<div[^>]*>/i', // </p> before <div>
  140. '/<br[^>]*>[\s\n\t]*<div[^>]*>/i', // <br> before <div>
  141. '/<br[^>]*>\s*/i', // <br>
  142. '/<i[^>]*>(.*?)<\/i>/i', // <i>
  143. '/<em[^>]*>(.*?)<\/em>/i', // <em>
  144. '/(<ul[^>]*>|<\/ul>)/i', // <ul> and </ul>
  145. '/(<ol[^>]*>|<\/ol>)/i', // <ol> and </ol>
  146. '/<li[^>]*>(.*?)<\/li>/i', // <li> and </li>
  147. '/<li[^>]*>/i', // <li>
  148. '/<hr[^>]*>/i', // <hr>
  149. '/<div[^>]*>/i', // <div>
  150. '/(<table[^>]*>|<\/table>)/i', // <table> and </table>
  151. '/(<tr[^>]*>|<\/tr>)/i', // <tr> and </tr>
  152. '/<td[^>]*>(.*?)<\/td>/i', // <td> and </td>
  153. );
  154. /**
  155. * List of pattern replacements corresponding to patterns searched.
  156. *
  157. * @var array $replace
  158. * @see $search
  159. */
  160. protected $replace = array(
  161. '', // Non-legal carriage return
  162. '', // Anything before <body>
  163. '', // <head>
  164. '', // <script>
  165. '', // <style>
  166. ' ', // Newlines and tabs
  167. "\n\n", // <p>
  168. "\n<div>", // </p> before <div>
  169. '<div>', // <br> before <div>
  170. "\n", // <br>
  171. '_\\1_', // <i>
  172. '_\\1_', // <em>
  173. "\n\n", // <ul> and </ul>
  174. "\n\n", // <ol> and </ol>
  175. "\t* \\1\n", // <li> and </li>
  176. "\n\t* ", // <li>
  177. "\n-------------------------\n", // <hr>
  178. "<div>\n", // <div>
  179. "\n\n", // <table> and </table>
  180. "\n", // <tr> and </tr>
  181. "\t\t\\1\n", // <td> and </td>
  182. );
  183. /**
  184. * List of preg* regular expression patterns to search for,
  185. * used in conjunction with $ent_replace.
  186. *
  187. * @var array $ent_search
  188. * @see $ent_replace
  189. */
  190. protected $ent_search = array(
  191. '/&(nbsp|#160);/i', // Non-breaking space
  192. '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i',
  193. // Double quotes
  194. '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes
  195. '/&gt;/i', // Greater-than
  196. '/&lt;/i', // Less-than
  197. '/&(copy|#169);/i', // Copyright
  198. '/&(trade|#8482|#153);/i', // Trademark
  199. '/&(reg|#174);/i', // Registered
  200. '/&(mdash|#151|#8212);/i', // mdash
  201. '/&(ndash|minus|#8211|#8722);/i', // ndash
  202. '/&(bull|#149|#8226);/i', // Bullet
  203. '/&(pound|#163);/i', // Pound sign
  204. '/&(euro|#8364);/i', // Euro sign
  205. '/&(amp|#38);/i', // Ampersand: see _converter()
  206. '/[ ]{2,}/', // Runs of spaces, post-handling
  207. );
  208. /**
  209. * List of pattern replacements corresponding to patterns searched.
  210. *
  211. * @var array $ent_replace
  212. * @see $ent_search
  213. */
  214. protected $ent_replace = array(
  215. "\xC2\xA0", // Non-breaking space
  216. '"', // Double quotes
  217. "'", // Single quotes
  218. '>',
  219. '<',
  220. '(c)',
  221. '(tm)',
  222. '(R)',
  223. '--',
  224. '-',
  225. '*',
  226. '£',
  227. 'EUR', // Euro sign. € ?
  228. '|+|amp|+|', // Ampersand: see _converter()
  229. ' ', // Runs of spaces, post-handling
  230. );
  231. /**
  232. * List of preg* regular expression patterns to search for
  233. * and replace using callback function.
  234. *
  235. * @var array $callback_search
  236. */
  237. protected $callback_search = array(
  238. '/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i', // <a href="">
  239. '/<(h)[123456]( [^>]*)?>(.*?)<\/h[123456]>/i', // h1 - h6
  240. '/<(b)( [^>]*)?>(.*?)<\/b>/i', // <b>
  241. '/<(strong)( [^>]*)?>(.*?)<\/strong>/i', // <strong>
  242. '/<(th)( [^>]*)?>(.*?)<\/th>/i', // <th> and </th>
  243. );
  244. /**
  245. * List of preg* regular expression patterns to search for in PRE body,
  246. * used in conjunction with $pre_replace.
  247. *
  248. * @var array $pre_search
  249. * @see $pre_replace
  250. */
  251. protected $pre_search = array(
  252. "/\n/",
  253. "/\t/",
  254. '/ /',
  255. '/<pre[^>]*>/',
  256. '/<\/pre>/'
  257. );
  258. /**
  259. * List of pattern replacements corresponding to patterns searched for PRE body.
  260. *
  261. * @var array $pre_replace
  262. * @see $pre_search
  263. */
  264. protected $pre_replace = array(
  265. '<br>',
  266. '&nbsp;&nbsp;&nbsp;&nbsp;',
  267. '&nbsp;',
  268. '',
  269. ''
  270. );
  271. /**
  272. * Contains a list of HTML tags to allow in the resulting text.
  273. *
  274. * @var string $allowed_tags
  275. * @see set_allowed_tags()
  276. */
  277. protected $allowed_tags = '';
  278. /**
  279. * Contains the base URL that relative links should resolve to.
  280. *
  281. * @var string $url
  282. */
  283. protected $url;
  284. /**
  285. * Indicates whether content in the $html variable has been converted yet.
  286. *
  287. * @var boolean $_converted
  288. * @see $html, $text
  289. */
  290. protected $_converted = false;
  291. /**
  292. * Contains URL addresses from links to be rendered in plain text.
  293. *
  294. * @var array $_link_list
  295. * @see _build_link_list()
  296. */
  297. protected $_link_list = array();
  298. /**
  299. * Boolean flag, true if a table of link URLs should be listed after the text.
  300. *
  301. * @var boolean $_do_links
  302. * @see __construct()
  303. */
  304. protected $_do_links = true;
  305. /**
  306. * Constructor.
  307. *
  308. * If the HTML source string (or file) is supplied, the class
  309. * will instantiate with that source propagated, all that has
  310. * to be done it to call get_text().
  311. *
  312. * @param string $source HTML content
  313. * @param boolean $from_file Indicates $source is a file to pull content from
  314. * @param boolean $do_links Indicate whether a table of link URLs is desired
  315. * @param integer $width Maximum width of the formatted text, 0 for no limit
  316. */
  317. function __construct($source = '', $from_file = false, $do_links = true, $width = 75, $charset = 'UTF-8')
  318. {
  319. if (!empty($source)) {
  320. $this->set_html($source, $from_file);
  321. }
  322. $this->set_base_url();
  323. $this->_do_links = $do_links;
  324. $this->width = $width;
  325. $this->charset = $charset;
  326. }
  327. /**
  328. * Loads source HTML into memory, either from $source string or a file.
  329. *
  330. * @param string $source HTML content
  331. * @param boolean $from_file Indicates $source is a file to pull content from
  332. */
  333. function set_html($source, $from_file = false)
  334. {
  335. if ($from_file && file_exists($source)) {
  336. $this->html = file_get_contents($source);
  337. }
  338. else {
  339. $this->html = $source;
  340. }
  341. $this->_converted = false;
  342. }
  343. /**
  344. * Returns the text, converted from HTML.
  345. *
  346. * @return string Plain text
  347. */
  348. function get_text()
  349. {
  350. if (!$this->_converted) {
  351. $this->_convert();
  352. }
  353. return $this->text;
  354. }
  355. /**
  356. * Prints the text, converted from HTML.
  357. */
  358. function print_text()
  359. {
  360. print $this->get_text();
  361. }
  362. /**
  363. * Sets the allowed HTML tags to pass through to the resulting text.
  364. *
  365. * Tags should be in the form "<p>", with no corresponding closing tag.
  366. */
  367. function set_allowed_tags($allowed_tags = '')
  368. {
  369. if (!empty($allowed_tags)) {
  370. $this->allowed_tags = $allowed_tags;
  371. }
  372. }
  373. /**
  374. * Sets a base URL to handle relative links.
  375. */
  376. function set_base_url($url = '')
  377. {
  378. if (empty($url)) {
  379. if (!empty($_SERVER['HTTP_HOST'])) {
  380. $this->url = 'http://' . $_SERVER['HTTP_HOST'];
  381. }
  382. else {
  383. $this->url = '';
  384. }
  385. }
  386. else {
  387. // Strip any trailing slashes for consistency (relative
  388. // URLs may already start with a slash like "/file.html")
  389. if (substr($url, -1) == '/') {
  390. $url = substr($url, 0, -1);
  391. }
  392. $this->url = $url;
  393. }
  394. }
  395. /**
  396. * Workhorse function that does actual conversion (calls _converter() method).
  397. */
  398. protected function _convert()
  399. {
  400. // Variables used for building the link list
  401. $this->_link_list = array();
  402. $text = $this->html;
  403. // Convert HTML to TXT
  404. $this->_converter($text);
  405. // Add link list
  406. if (!empty($this->_link_list)) {
  407. $text .= "\n\nLinks:\n------\n";
  408. foreach ($this->_link_list as $idx => $url) {
  409. $text .= '[' . ($idx+1) . '] ' . $url . "\n";
  410. }
  411. }
  412. $this->text = $text;
  413. $this->_converted = true;
  414. }
  415. /**
  416. * Workhorse function that does actual conversion.
  417. *
  418. * First performs custom tag replacement specified by $search and
  419. * $replace arrays. Then strips any remaining HTML tags, reduces whitespace
  420. * and newlines to a readable format, and word wraps the text to
  421. * $width characters.
  422. *
  423. * @param string Reference to HTML content string
  424. */
  425. protected function _converter(&$text)
  426. {
  427. // Convert <BLOCKQUOTE> (before PRE!)
  428. $this->_convert_blockquotes($text);
  429. // Convert <PRE>
  430. $this->_convert_pre($text);
  431. // Run our defined tags search-and-replace
  432. $text = preg_replace($this->search, $this->replace, $text);
  433. // Run our defined tags search-and-replace with callback
  434. $text = preg_replace_callback($this->callback_search, array($this, 'tags_preg_callback'), $text);
  435. // Strip any other HTML tags
  436. $text = strip_tags($text, $this->allowed_tags);
  437. // Run our defined entities/characters search-and-replace
  438. $text = preg_replace($this->ent_search, $this->ent_replace, $text);
  439. // Replace known html entities
  440. $text = html_entity_decode($text, ENT_QUOTES, $this->charset);
  441. // Replace unicode nbsp to regular spaces
  442. $text = preg_replace('/\xC2\xA0/', ' ', $text);
  443. // Remove unknown/unhandled entities (this cannot be done in search-and-replace block)
  444. $text = preg_replace('/&([a-zA-Z0-9]{2,6}|#[0-9]{2,4});/', '', $text);
  445. // Convert "|+|amp|+|" into "&", need to be done after handling of unknown entities
  446. // This properly handles situation of "&amp;quot;" in input string
  447. $text = str_replace('|+|amp|+|', '&', $text);
  448. // Bring down number of empty lines to 2 max
  449. $text = preg_replace("/\n\s+\n/", "\n\n", $text);
  450. $text = preg_replace("/[\n]{3,}/", "\n\n", $text);
  451. // remove leading empty lines (can be produced by eg. P tag on the beginning)
  452. $text = ltrim($text, "\n");
  453. // Wrap the text to a readable format
  454. // for PHP versions >= 4.0.2. Default width is 75
  455. // If width is 0 or less, don't wrap the text.
  456. if ( $this->width > 0 ) {
  457. $text = wordwrap($text, $this->width);
  458. }
  459. }
  460. /**
  461. * Helper function called by preg_replace() on link replacement.
  462. *
  463. * Maintains an internal list of links to be displayed at the end of the
  464. * text, with numeric indices to the original point in the text they
  465. * appeared. Also makes an effort at identifying and handling absolute
  466. * and relative links.
  467. *
  468. * @param string $link URL of the link
  469. * @param string $display Part of the text to associate number with
  470. */
  471. protected function _build_link_list($link, $display)
  472. {
  473. if (!$this->_do_links || empty($link)) {
  474. return $display;
  475. }
  476. // Ignored link types
  477. if (preg_match('!^(javascript:|mailto:|#)!i', $link)) {
  478. return $display;
  479. }
  480. // skip links with href == content (#1490434)
  481. if ($link === $display) {
  482. return $display;
  483. }
  484. if (preg_match('!^([a-z][a-z0-9.+-]+:)!i', $link)) {
  485. $url = $link;
  486. }
  487. else {
  488. $url = $this->url;
  489. if (substr($link, 0, 1) != '/') {
  490. $url .= '/';
  491. }
  492. $url .= "$link";
  493. }
  494. if (($index = array_search($url, $this->_link_list)) === false) {
  495. $index = count($this->_link_list);
  496. $this->_link_list[] = $url;
  497. }
  498. return $display . ' [' . ($index+1) . ']';
  499. }
  500. /**
  501. * Helper function for PRE body conversion.
  502. *
  503. * @param string HTML content
  504. */
  505. protected function _convert_pre(&$text)
  506. {
  507. // get the content of PRE element
  508. while (preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches)) {
  509. $this->pre_content = $matches[1];
  510. // Run our defined tags search-and-replace with callback
  511. $this->pre_content = preg_replace_callback($this->callback_search,
  512. array($this, 'tags_preg_callback'), $this->pre_content);
  513. // convert the content
  514. $this->pre_content = sprintf('<div><br>%s<br></div>',
  515. preg_replace($this->pre_search, $this->pre_replace, $this->pre_content));
  516. // replace the content (use callback because content can contain $0 variable)
  517. $text = preg_replace_callback('/<pre[^>]*>.*<\/pre>/ismU',
  518. array($this, 'pre_preg_callback'), $text, 1);
  519. // free memory
  520. $this->pre_content = '';
  521. }
  522. }
  523. /**
  524. * Helper function for BLOCKQUOTE body conversion.
  525. *
  526. * @param string HTML content
  527. */
  528. protected function _convert_blockquotes(&$text)
  529. {
  530. $level = 0;
  531. $offset = 0;
  532. while (($start = stripos($text, '<blockquote', $offset)) !== false) {
  533. $offset = $start + 12;
  534. do {
  535. $end = stripos($text, '</blockquote>', $offset);
  536. $next = stripos($text, '<blockquote', $offset);
  537. // nested <blockquote>, skip
  538. if ($next !== false && $next < $end) {
  539. $offset = $next + 12;
  540. $level++;
  541. }
  542. // nested </blockquote> tag
  543. if ($end !== false && $level > 0) {
  544. $offset = $end + 12;
  545. $level--;
  546. }
  547. // found matching end tag
  548. else if ($end !== false && $level == 0) {
  549. $taglen = strpos($text, '>', $start) - $start;
  550. $startpos = $start + $taglen + 1;
  551. // get blockquote content
  552. $body = trim(substr($text, $startpos, $end - $startpos));
  553. // adjust text wrapping width
  554. $p_width = $this->width;
  555. if ($this->width > 0) $this->width -= 2;
  556. // replace content with inner blockquotes
  557. $this->_converter($body);
  558. // resore text width
  559. $this->width = $p_width;
  560. // Add citation markers and create <pre> block
  561. $body = preg_replace_callback('/((?:^|\n)>*)([^\n]*)/', array($this, 'blockquote_citation_callback'), trim($body));
  562. $body = '<pre>' . htmlspecialchars($body) . '</pre>';
  563. $text = substr_replace($text, $body . "\n", $start, $end + 13 - $start);
  564. $offset = 0;
  565. break;
  566. }
  567. // abort on invalid tag structure (e.g. no closing tag found)
  568. else {
  569. break;
  570. }
  571. }
  572. while ($end || $next);
  573. }
  574. }
  575. /**
  576. * Callback function to correctly add citation markers for blockquote contents
  577. */
  578. public function blockquote_citation_callback($m)
  579. {
  580. $line = ltrim($m[2]);
  581. $space = $line[0] == '>' ? '' : ' ';
  582. return $m[1] . '>' . $space . $line;
  583. }
  584. /**
  585. * Callback function for preg_replace_callback use.
  586. *
  587. * @param array PREG matches
  588. * @return string
  589. */
  590. public function tags_preg_callback($matches)
  591. {
  592. switch (strtolower($matches[1])) {
  593. case 'b':
  594. case 'strong':
  595. return $this->_toupper($matches[3]);
  596. case 'th':
  597. return $this->_toupper("\t\t". $matches[3] ."\n");
  598. case 'h':
  599. return $this->_toupper("\n\n". $matches[3] ."\n\n");
  600. case 'a':
  601. // Remove spaces in URL (#1487805)
  602. $url = str_replace(' ', '', $matches[3]);
  603. return $this->_build_link_list($url, $matches[4]);
  604. }
  605. }
  606. /**
  607. * Callback function for preg_replace_callback use in PRE content handler.
  608. *
  609. * @param array PREG matches
  610. * @return string
  611. */
  612. public function pre_preg_callback($matches)
  613. {
  614. return $this->pre_content;
  615. }
  616. /**
  617. * Strtoupper function with HTML tags and entities handling.
  618. *
  619. * @param string $str Text to convert
  620. * @return string Converted text
  621. */
  622. private function _toupper($str)
  623. {
  624. // string can containg HTML tags
  625. $chunks = preg_split('/(<[^>]*>)/', $str, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  626. // convert toupper only the text between HTML tags
  627. foreach ($chunks as $idx => $chunk) {
  628. if ($chunk[0] != '<') {
  629. $chunks[$idx] = $this->_strtoupper($chunk);
  630. }
  631. }
  632. return implode($chunks);
  633. }
  634. /**
  635. * Strtoupper multibyte wrapper function with HTML entities handling.
  636. *
  637. * @param string $str Text to convert
  638. * @return string Converted text
  639. */
  640. private function _strtoupper($str)
  641. {
  642. $str = html_entity_decode($str, ENT_COMPAT, $this->charset);
  643. $str = mb_strtoupper($str);
  644. $str = htmlspecialchars($str, ENT_COMPAT, $this->charset);
  645. return $str;
  646. }
  647. }