ExportLatex.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. /**
  3. * Set of methods used to build dumps of tables as Latex
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Plugins\ExportPlugin;
  9. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  10. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  11. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  12. use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
  13. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  14. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  15. use PhpMyAdmin\Util;
  16. use PhpMyAdmin\Version;
  17. use function __;
  18. use function count;
  19. use function in_array;
  20. use function mb_strpos;
  21. use function mb_substr;
  22. use function str_replace;
  23. use function stripslashes;
  24. use const PHP_VERSION;
  25. /**
  26. * Handles the export for the Latex format
  27. */
  28. class ExportLatex extends ExportPlugin
  29. {
  30. /**
  31. * @psalm-return non-empty-lowercase-string
  32. */
  33. public function getName(): string
  34. {
  35. return 'latex';
  36. }
  37. /**
  38. * Initialize the local variables that are used for export Latex.
  39. */
  40. protected function init(): void
  41. {
  42. /* Messages used in default captions */
  43. $GLOBALS['strLatexContent'] = __('Content of table @TABLE@');
  44. $GLOBALS['strLatexContinued'] = __('(continued)');
  45. $GLOBALS['strLatexStructure'] = __('Structure of table @TABLE@');
  46. }
  47. protected function setProperties(): ExportPluginProperties
  48. {
  49. global $plugin_param;
  50. $hide_structure = false;
  51. if ($plugin_param['export_type'] === 'table' && ! $plugin_param['single_table']) {
  52. $hide_structure = true;
  53. }
  54. $exportPluginProperties = new ExportPluginProperties();
  55. $exportPluginProperties->setText('LaTeX');
  56. $exportPluginProperties->setExtension('tex');
  57. $exportPluginProperties->setMimeType('application/x-tex');
  58. $exportPluginProperties->setOptionsText(__('Options'));
  59. // create the root group that will be the options field for
  60. // $exportPluginProperties
  61. // this will be shown as "Format specific options"
  62. $exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
  63. // general options main group
  64. $generalOptions = new OptionsPropertyMainGroup('general_opts');
  65. // create primary items and add them to the group
  66. $leaf = new BoolPropertyItem(
  67. 'caption',
  68. __('Include table caption')
  69. );
  70. $generalOptions->addProperty($leaf);
  71. // add the main group to the root group
  72. $exportSpecificOptions->addProperty($generalOptions);
  73. // what to dump (structure/data/both) main group
  74. $dumpWhat = new OptionsPropertyMainGroup(
  75. 'dump_what',
  76. __('Dump table')
  77. );
  78. // create primary items and add them to the group
  79. $leaf = new RadioPropertyItem('structure_or_data');
  80. $leaf->setValues(
  81. [
  82. 'structure' => __('structure'),
  83. 'data' => __('data'),
  84. 'structure_and_data' => __('structure and data'),
  85. ]
  86. );
  87. $dumpWhat->addProperty($leaf);
  88. // add the main group to the root group
  89. $exportSpecificOptions->addProperty($dumpWhat);
  90. // structure options main group
  91. if (! $hide_structure) {
  92. $structureOptions = new OptionsPropertyMainGroup(
  93. 'structure',
  94. __('Object creation options')
  95. );
  96. $structureOptions->setForce('data');
  97. // create primary items and add them to the group
  98. $leaf = new TextPropertyItem(
  99. 'structure_caption',
  100. __('Table caption:')
  101. );
  102. $leaf->setDoc('faq6-27');
  103. $structureOptions->addProperty($leaf);
  104. $leaf = new TextPropertyItem(
  105. 'structure_continued_caption',
  106. __('Table caption (continued):')
  107. );
  108. $leaf->setDoc('faq6-27');
  109. $structureOptions->addProperty($leaf);
  110. $leaf = new TextPropertyItem(
  111. 'structure_label',
  112. __('Label key:')
  113. );
  114. $leaf->setDoc('faq6-27');
  115. $structureOptions->addProperty($leaf);
  116. $relationParameters = $this->relation->getRelationParameters();
  117. if ($relationParameters->relationFeature !== null) {
  118. $leaf = new BoolPropertyItem(
  119. 'relation',
  120. __('Display foreign key relationships')
  121. );
  122. $structureOptions->addProperty($leaf);
  123. }
  124. $leaf = new BoolPropertyItem(
  125. 'comments',
  126. __('Display comments')
  127. );
  128. $structureOptions->addProperty($leaf);
  129. if ($relationParameters->browserTransformationFeature !== null) {
  130. $leaf = new BoolPropertyItem(
  131. 'mime',
  132. __('Display media types')
  133. );
  134. $structureOptions->addProperty($leaf);
  135. }
  136. // add the main group to the root group
  137. $exportSpecificOptions->addProperty($structureOptions);
  138. }
  139. // data options main group
  140. $dataOptions = new OptionsPropertyMainGroup(
  141. 'data',
  142. __('Data dump options')
  143. );
  144. $dataOptions->setForce('structure');
  145. // create primary items and add them to the group
  146. $leaf = new BoolPropertyItem(
  147. 'columns',
  148. __('Put columns names in the first row:')
  149. );
  150. $dataOptions->addProperty($leaf);
  151. $leaf = new TextPropertyItem(
  152. 'data_caption',
  153. __('Table caption:')
  154. );
  155. $leaf->setDoc('faq6-27');
  156. $dataOptions->addProperty($leaf);
  157. $leaf = new TextPropertyItem(
  158. 'data_continued_caption',
  159. __('Table caption (continued):')
  160. );
  161. $leaf->setDoc('faq6-27');
  162. $dataOptions->addProperty($leaf);
  163. $leaf = new TextPropertyItem(
  164. 'data_label',
  165. __('Label key:')
  166. );
  167. $leaf->setDoc('faq6-27');
  168. $dataOptions->addProperty($leaf);
  169. $leaf = new TextPropertyItem(
  170. 'null',
  171. __('Replace NULL with:')
  172. );
  173. $dataOptions->addProperty($leaf);
  174. // add the main group to the root group
  175. $exportSpecificOptions->addProperty($dataOptions);
  176. // set the options for the export plugin property item
  177. $exportPluginProperties->setOptions($exportSpecificOptions);
  178. return $exportPluginProperties;
  179. }
  180. /**
  181. * Outputs export header
  182. */
  183. public function exportHeader(): bool
  184. {
  185. global $crlf, $cfg, $dbi;
  186. $head = '% phpMyAdmin LaTeX Dump' . $crlf
  187. . '% version ' . Version::VERSION . $crlf
  188. . '% https://www.phpmyadmin.net/' . $crlf
  189. . '%' . $crlf
  190. . '% ' . __('Host:') . ' ' . $cfg['Server']['host'];
  191. if (! empty($cfg['Server']['port'])) {
  192. $head .= ':' . $cfg['Server']['port'];
  193. }
  194. $head .= $crlf
  195. . '% ' . __('Generation Time:') . ' '
  196. . Util::localisedDate() . $crlf
  197. . '% ' . __('Server version:') . ' ' . $dbi->getVersionString() . $crlf
  198. . '% ' . __('PHP Version:') . ' ' . PHP_VERSION . $crlf;
  199. return $this->export->outputHandler($head);
  200. }
  201. /**
  202. * Outputs export footer
  203. */
  204. public function exportFooter(): bool
  205. {
  206. return true;
  207. }
  208. /**
  209. * Outputs database header
  210. *
  211. * @param string $db Database name
  212. * @param string $dbAlias Aliases of db
  213. */
  214. public function exportDBHeader($db, $dbAlias = ''): bool
  215. {
  216. if (empty($dbAlias)) {
  217. $dbAlias = $db;
  218. }
  219. global $crlf;
  220. $head = '% ' . $crlf
  221. . '% ' . __('Database:') . ' \'' . $dbAlias . '\'' . $crlf
  222. . '% ' . $crlf;
  223. return $this->export->outputHandler($head);
  224. }
  225. /**
  226. * Outputs database footer
  227. *
  228. * @param string $db Database name
  229. */
  230. public function exportDBFooter($db): bool
  231. {
  232. return true;
  233. }
  234. /**
  235. * Outputs CREATE DATABASE statement
  236. *
  237. * @param string $db Database name
  238. * @param string $exportType 'server', 'database', 'table'
  239. * @param string $dbAlias Aliases of db
  240. */
  241. public function exportDBCreate($db, $exportType, $dbAlias = ''): bool
  242. {
  243. return true;
  244. }
  245. /**
  246. * Outputs the content of a table in JSON format
  247. *
  248. * @param string $db database name
  249. * @param string $table table name
  250. * @param string $crlf the end of line sequence
  251. * @param string $errorUrl the url to go back in case of error
  252. * @param string $sqlQuery SQL query for obtaining data
  253. * @param array $aliases Aliases of db/table/columns
  254. */
  255. public function exportData(
  256. $db,
  257. $table,
  258. $crlf,
  259. $errorUrl,
  260. $sqlQuery,
  261. array $aliases = []
  262. ): bool {
  263. global $dbi;
  264. $db_alias = $db;
  265. $table_alias = $table;
  266. $this->initAlias($aliases, $db_alias, $table_alias);
  267. $result = $dbi->tryQuery($sqlQuery, DatabaseInterface::CONNECT_USER, DatabaseInterface::QUERY_UNBUFFERED);
  268. $columns_cnt = $result->numFields();
  269. $columns = [];
  270. $columns_alias = [];
  271. foreach ($result->getFieldNames() as $i => $col_as) {
  272. $columns[$i] = $col_as;
  273. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  274. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  275. }
  276. $columns_alias[$i] = $col_as;
  277. }
  278. $buffer = $crlf . '%' . $crlf . '% ' . __('Data:') . ' ' . $table_alias
  279. . $crlf . '%' . $crlf . ' \\begin{longtable}{|';
  280. for ($index = 0; $index < $columns_cnt; $index++) {
  281. $buffer .= 'l|';
  282. }
  283. $buffer .= '} ' . $crlf;
  284. $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
  285. if (isset($GLOBALS['latex_caption'])) {
  286. $buffer .= ' \\caption{'
  287. . Util::expandUserString(
  288. $GLOBALS['latex_data_caption'],
  289. [
  290. 'texEscape',
  291. static::class,
  292. ],
  293. [
  294. 'table' => $table_alias,
  295. 'database' => $db_alias,
  296. ]
  297. )
  298. . '} \\label{'
  299. . Util::expandUserString(
  300. $GLOBALS['latex_data_label'],
  301. null,
  302. [
  303. 'table' => $table_alias,
  304. 'database' => $db_alias,
  305. ]
  306. )
  307. . '} \\\\';
  308. }
  309. if (! $this->export->outputHandler($buffer)) {
  310. return false;
  311. }
  312. // show column names
  313. if (isset($GLOBALS['latex_columns'])) {
  314. $buffer = '\\hline ';
  315. for ($i = 0; $i < $columns_cnt; $i++) {
  316. $buffer .= '\\multicolumn{1}{|c|}{\\textbf{'
  317. . self::texEscape(stripslashes($columns_alias[$i])) . '}} & ';
  318. }
  319. $buffer = mb_substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
  320. if (! $this->export->outputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
  321. return false;
  322. }
  323. if (isset($GLOBALS['latex_caption'])) {
  324. if (
  325. ! $this->export->outputHandler(
  326. '\\caption{'
  327. . Util::expandUserString(
  328. $GLOBALS['latex_data_continued_caption'],
  329. [
  330. 'texEscape',
  331. static::class,
  332. ],
  333. [
  334. 'table' => $table_alias,
  335. 'database' => $db_alias,
  336. ]
  337. )
  338. . '} \\\\ '
  339. )
  340. ) {
  341. return false;
  342. }
  343. }
  344. if (! $this->export->outputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
  345. return false;
  346. }
  347. } else {
  348. if (! $this->export->outputHandler('\\\\ \hline')) {
  349. return false;
  350. }
  351. }
  352. // print the whole table
  353. while ($record = $result->fetchAssoc()) {
  354. $buffer = '';
  355. // print each row
  356. for ($i = 0; $i < $columns_cnt; $i++) {
  357. if ($record[$columns[$i]] !== null && isset($record[$columns[$i]])) {
  358. $column_value = self::texEscape(
  359. stripslashes($record[$columns[$i]])
  360. );
  361. } else {
  362. $column_value = $GLOBALS['latex_null'];
  363. }
  364. // last column ... no need for & character
  365. if ($i == $columns_cnt - 1) {
  366. $buffer .= $column_value;
  367. } else {
  368. $buffer .= $column_value . ' & ';
  369. }
  370. }
  371. $buffer .= ' \\\\ \\hline ' . $crlf;
  372. if (! $this->export->outputHandler($buffer)) {
  373. return false;
  374. }
  375. }
  376. $buffer = ' \\end{longtable}' . $crlf;
  377. return $this->export->outputHandler($buffer);
  378. }
  379. /**
  380. * Outputs result raw query
  381. *
  382. * @param string $errorUrl the url to go back in case of error
  383. * @param string|null $db the database where the query is executed
  384. * @param string $sqlQuery the rawquery to output
  385. * @param string $crlf the end of line sequence
  386. */
  387. public function exportRawQuery(string $errorUrl, ?string $db, string $sqlQuery, string $crlf): bool
  388. {
  389. global $dbi;
  390. if ($db !== null) {
  391. $dbi->selectDb($db);
  392. }
  393. return $this->exportData($db ?? '', '', $crlf, $errorUrl, $sqlQuery);
  394. }
  395. /**
  396. * Outputs table's structure
  397. *
  398. * @param string $db database name
  399. * @param string $table table name
  400. * @param string $crlf the end of line sequence
  401. * @param string $errorUrl the url to go back in case of error
  402. * @param string $exportMode 'create_table', 'triggers', 'create_view',
  403. * 'stand_in'
  404. * @param string $exportType 'server', 'database', 'table'
  405. * @param bool $do_relation whether to include relation comments
  406. * @param bool $do_comments whether to include the pmadb-style column
  407. * comments as comments in the structure;
  408. * this is deprecated but the parameter is
  409. * left here because /export calls
  410. * exportStructure() also for other
  411. * export types which use this parameter
  412. * @param bool $do_mime whether to include mime comments
  413. * @param bool $dates whether to include creation/update/check dates
  414. * @param array $aliases Aliases of db/table/columns
  415. */
  416. public function exportStructure(
  417. $db,
  418. $table,
  419. $crlf,
  420. $errorUrl,
  421. $exportMode,
  422. $exportType,
  423. $do_relation = false,
  424. $do_comments = false,
  425. $do_mime = false,
  426. $dates = false,
  427. array $aliases = []
  428. ): bool {
  429. global $dbi;
  430. $db_alias = $db;
  431. $table_alias = $table;
  432. $this->initAlias($aliases, $db_alias, $table_alias);
  433. $relationParameters = $this->relation->getRelationParameters();
  434. /* We do not export triggers */
  435. if ($exportMode === 'triggers') {
  436. return true;
  437. }
  438. /**
  439. * Get the unique keys in the table
  440. */
  441. $unique_keys = [];
  442. $keys = $dbi->getTableIndexes($db, $table);
  443. foreach ($keys as $key) {
  444. if ($key['Non_unique'] != 0) {
  445. continue;
  446. }
  447. $unique_keys[] = $key['Column_name'];
  448. }
  449. /**
  450. * Gets fields properties
  451. */
  452. $dbi->selectDb($db);
  453. // Check if we can use Relations
  454. [$res_rel, $have_rel] = $this->relation->getRelationsAndStatus(
  455. $do_relation && $relationParameters->relationFeature !== null,
  456. $db,
  457. $table
  458. );
  459. /**
  460. * Displays the table structure
  461. */
  462. $buffer = $crlf . '%' . $crlf . '% ' . __('Structure:') . ' '
  463. . $table_alias . $crlf . '%' . $crlf . ' \\begin{longtable}{';
  464. if (! $this->export->outputHandler($buffer)) {
  465. return false;
  466. }
  467. $alignment = '|l|c|c|c|';
  468. if ($do_relation && $have_rel) {
  469. $alignment .= 'l|';
  470. }
  471. if ($do_comments) {
  472. $alignment .= 'l|';
  473. }
  474. if ($do_mime && $relationParameters->browserTransformationFeature !== null) {
  475. $alignment .= 'l|';
  476. }
  477. $buffer = $alignment . '} ' . $crlf;
  478. $header = ' \\hline ';
  479. $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column')
  480. . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type')
  481. . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null')
  482. . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}';
  483. if ($do_relation && $have_rel) {
  484. $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}';
  485. }
  486. if ($do_comments) {
  487. $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}';
  488. $comments = $this->relation->getComments($db, $table);
  489. }
  490. if ($do_mime && $relationParameters->browserTransformationFeature !== null) {
  491. $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
  492. $mime_map = $this->transformations->getMime($db, $table, true);
  493. }
  494. // Table caption for first page and label
  495. if (isset($GLOBALS['latex_caption'])) {
  496. $buffer .= ' \\caption{'
  497. . Util::expandUserString(
  498. $GLOBALS['latex_structure_caption'],
  499. [
  500. 'texEscape',
  501. static::class,
  502. ],
  503. [
  504. 'table' => $table_alias,
  505. 'database' => $db_alias,
  506. ]
  507. )
  508. . '} \\label{'
  509. . Util::expandUserString(
  510. $GLOBALS['latex_structure_label'],
  511. null,
  512. [
  513. 'table' => $table_alias,
  514. 'database' => $db_alias,
  515. ]
  516. )
  517. . '} \\\\' . $crlf;
  518. }
  519. $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf
  520. . '\\endfirsthead' . $crlf;
  521. // Table caption on next pages
  522. if (isset($GLOBALS['latex_caption'])) {
  523. $buffer .= ' \\caption{'
  524. . Util::expandUserString(
  525. $GLOBALS['latex_structure_continued_caption'],
  526. [
  527. 'texEscape',
  528. static::class,
  529. ],
  530. [
  531. 'table' => $table_alias,
  532. 'database' => $db_alias,
  533. ]
  534. )
  535. . '} \\\\ ' . $crlf;
  536. }
  537. $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
  538. if (! $this->export->outputHandler($buffer)) {
  539. return false;
  540. }
  541. $fields = $dbi->getColumns($db, $table);
  542. foreach ($fields as $row) {
  543. $extracted_columnspec = Util::extractColumnSpec($row['Type']);
  544. $type = $extracted_columnspec['print_type'];
  545. if (empty($type)) {
  546. $type = ' ';
  547. }
  548. if (! isset($row['Default'])) {
  549. if ($row['Null'] !== 'NO') {
  550. $row['Default'] = 'NULL';
  551. }
  552. }
  553. $field_name = $col_as = $row['Field'];
  554. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  555. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  556. }
  557. $local_buffer = $col_as . "\000" . $type . "\000"
  558. . ($row['Null'] == '' || $row['Null'] === 'NO'
  559. ? __('No') : __('Yes'))
  560. . "\000" . ($row['Default'] ?? '');
  561. if ($do_relation && $have_rel) {
  562. $local_buffer .= "\000";
  563. $local_buffer .= $this->getRelationString($res_rel, $field_name, $db, $aliases);
  564. }
  565. if ($do_comments && $relationParameters->columnCommentsFeature !== null) {
  566. $local_buffer .= "\000";
  567. if (isset($comments[$field_name])) {
  568. $local_buffer .= $comments[$field_name];
  569. }
  570. }
  571. if ($do_mime && $relationParameters->browserTransformationFeature !== null) {
  572. $local_buffer .= "\000";
  573. if (isset($mime_map[$field_name])) {
  574. $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
  575. }
  576. }
  577. $local_buffer = self::texEscape($local_buffer);
  578. if ($row['Key'] === 'PRI') {
  579. $pos = (int) mb_strpos($local_buffer, "\000");
  580. $local_buffer = '\\textit{'
  581. .
  582. mb_substr($local_buffer, 0, $pos)
  583. . '}' .
  584. mb_substr($local_buffer, $pos);
  585. }
  586. if (in_array($field_name, $unique_keys)) {
  587. $pos = (int) mb_strpos($local_buffer, "\000");
  588. $local_buffer = '\\textbf{'
  589. .
  590. mb_substr($local_buffer, 0, $pos)
  591. . '}' .
  592. mb_substr($local_buffer, $pos);
  593. }
  594. $buffer = str_replace("\000", ' & ', $local_buffer);
  595. $buffer .= ' \\\\ \\hline ' . $crlf;
  596. if (! $this->export->outputHandler($buffer)) {
  597. return false;
  598. }
  599. }
  600. $buffer = ' \\end{longtable}' . $crlf;
  601. return $this->export->outputHandler($buffer);
  602. }
  603. /**
  604. * Escapes some special characters for use in TeX/LaTeX
  605. *
  606. * @param string $string the string to convert
  607. *
  608. * @return string the converted string with escape codes
  609. */
  610. public static function texEscape($string)
  611. {
  612. $escape = [
  613. '$',
  614. '%',
  615. '{',
  616. '}',
  617. '&',
  618. '#',
  619. '_',
  620. '^',
  621. ];
  622. $cnt_escape = count($escape);
  623. for ($k = 0; $k < $cnt_escape; $k++) {
  624. $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
  625. }
  626. return $string;
  627. }
  628. }