Last active
September 20, 2017 09:46
-
-
Save Dinir/7bb14c452486c17b78fc1c4fce832329 to your computer and use it in GitHub Desktop.
Format a copy-pasted table data text from Hangul Word Processor as an array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Format a copy-pasted table data text from Hangul Word Processor as an array. | |
| * | |
| * A pasted text is formatted like this: | |
| * \nA1\nB1\nC1\nA2\nB2\nC2\nA3\nB3\nC3\n | |
| * | |
| * with an appropriate regex used as a delimiter, e.g. '/\R(?!A)/', | |
| * this function can format the text to an array: | |
| * [['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ['A3', 'B3', 'C3']] | |
| * | |
| * @param string $text the pasted text from a table in an HWP document | |
| * @param string $row_delimiter the delimiter to divide each row in the text | |
| * @return array two dimentional array made from the pasted text | |
| */ | |
| $format_hangul_table_as_array = function($text, $row_delimiter) { | |
| if (empty($row_delimiter) || !is_string($row_delimiter)) { | |
| $row_del = '/\R/'; | |
| } else { | |
| $row_del = $row_delimiter; | |
| } | |
| $array = explode("\n", mb_ereg_replace($row_del, "\t", $text)); | |
| foreach ($array as &$row) { | |
| $row = array_filter(array_map('trim', explode("\t", $row)), 'strlen'); | |
| } | |
| $array = array_values(array_filter($array, 'count')); | |
| if(is_numeric($row_delimiter)) { | |
| $array = array_chunk($array, $row_delimiter); | |
| foreach($array as &$row) { | |
| array_walk($row, function(&$v) { | |
| $v = $v[0]; | |
| }); | |
| } | |
| } | |
| return $array; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage