Skip to content

Instantly share code, notes, and snippets.

@Dinir
Last active September 20, 2017 09:46
Show Gist options
  • Save Dinir/7bb14c452486c17b78fc1c4fce832329 to your computer and use it in GitHub Desktop.
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.
<?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;
};
@Dinir
Copy link
Author

Dinir commented Jul 6, 2017

Usage

$hangul_table = <<<RAW

A1
B1
C1
A2
B2
C2
A3
B3
C3
 
RAW;
$hangul_table = format_hangul_table_as_array($hangul_table, '/\R(?!A)/');
var_dump($hangul_table);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment