Reputation: 5755
I have a complex string that represents database tables. And I need to extract that database tables separately to process them.
Here's the string example:
First table
| | {{Категория}} | | {{Стоимость курсов}} | {{Стоимость учебного набора}} |
| 1 | Взрослый | 1 уровень = 50ч | 1~500 лей | 15 евро |
| 2 | Студент, Мастерант, Докторант | 1 уровень = 50ч | 1~000 лей | 15 евро |
| 3 | Ученик | 1 уровень = 50ч | 1~000 лей | 15 евро |
| 4 | Пенсионер | 1 уровень = 50ч | 1~000 лей | 15 евро |
text text text text text text
Second table:
| | {{Вид курсов}} | | {{Стоимость курсов}}| {{Стоимость учебного набора}} |
| 1 | dfgdfgdfg | 1 модуль | 500 лей | 0 |
|^|^| 2 модуля | 900 лей | 0 |
|^|^| 4 модуля | 1~500 лей | 0 |
| 2 | fgdfgdfg | 12ч | 800 лей | 0 |
| 3 | dfgdfgdfgdfg| 12ч | 900 лей | 0 |
|^|^| Предварительный тест | 400 лей | 0 |
text text text text text text
I tried using this regexp: \|.+
but preg_match_all()
simply dumps all the tables unseparated in the array. Any help, please? Thanks.
Upvotes: 0
Views: 167
Reputation: 12245
As i see, you have a set of tables within one string. And you need to split string to tables. I assume you could split the string with the text, separating tables.
<?php
$s = <<<EOSTR
First table
| | {{Категория}} | | {{Стоимость курсов}} | {{Стоимость учебного набора}} |
| 1 | Взрослый | 1 уровень = 50ч | 1~500 лей | 15 евро |
| 2 | Студент, Мастерант, Докторант | 1 уровень = 50ч | 1~000 лей | 15 евро |
| 3 | Ученик | 1 уровень = 50ч | 1~000 лей | 15 евро |
| 4 | Пенсионер | 1 уровень = 50ч | 1~000 лей | 15 евро |
text text text text text text
Second table:
| | {{Вид курсов}} | | {{Стоимость курсов}}| {{Стоимость учебного набора}} |
| 1 | dfgdfgdfg | 1 модуль | 500 лей | 0 |
|^|^| 2 модуля | 900 лей | 0 |
|^|^| 4 модуля | 1~500 лей | 0 |
| 2 | fgdfgdfg | 12ч | 800 лей | 0 |
| 3 | dfgdfgdfgdfg| 12ч | 900 лей | 0 |
|^|^| Предварительный тест | 400 лей | 0 |
text text text text text text
EOSTR;
$a = null;
$a = preg_split('/^(?:.(?<!\|))*$/xm', $s);
var_dump($a);
Just like here: http://ideone.com/VCt4f (using this question). This will give you this:
array(5) {
[0]=>
string(0) ""
[1]=>
string(506) "
| | {{Категория}} | | {{Стоимость курсов}} | {{Стоимость учебного набора}} |
| 1 | Взрослый | 1 уровень = 50ч | 1~500 лей | 15 евро |
| 2 | Студент, Мастерант, Докторант | 1 уровень = 50ч | 1~000 лей | 15 евро |
| 3 | Ученик | 1 уровень = 50ч | 1~000 лей | 15 евро |
| 4 | Пенсионер | 1 уровень = 50ч | 1~000 лей | 15 евро |
"
[2]=>
string(1) "
"
[3]=>
string(466) "
| | {{Вид курсов}} | | {{Стоимость курсов}}| {{Стоимость учебного набора}} |
| 1 | dfgdfgdfg | 1 модуль | 500 лей | 0 |
|^|^| 2 модуля | 900 лей | 0 |
|^|^| 4 модуля | 1~500 лей | 0 |
| 2 | fgdfgdfg | 12ч | 800 лей | 0 |
| 3 | dfgdfgdfgdfg| 12ч | 900 лей | 0 |
|^|^| Предварительный тест | 400 лей | 0 |
"
[4]=>
string(0) ""
}
When you're done extracting tables you could simply split them to columns with
// $a = preg_split...
foreach ($a as $table) {
if (!strlen(trim($table)))
continue;
$rows = preg_split('/\n/', $table);
foreach ($rows as $row) {
if (!strlen(trim($row)))
continue;
$columns = preg_split('/\|/', $row);
// work with $columns array
}
}
Upvotes: 1
Reputation: 1055
It's simpler and faster just to exchange the regexp with two explodes and an iteration
$table = array();
$rows = explode("\n", $string);
foreach($rows as $row)
$table[] = explode("|", $row);
Now we have a 2d array structure so that we easily can echo out as a table, like this
echo "<table>";
foreach($table as $row) {
echo "<tr>";
foreach($row as $column)
echo "<td>$column</td>";
echo "</tr>";
}
echo "</table>";
Upvotes: 0
Reputation: 5198
You could try using the explode() function on "|" and " " (space) and parse it that way. Other then that, I cant really think of any other way.
$exploded = explode("|", $table);
$exploded = explode(" ", $table);
Upvotes: 0