A
A
Alexey2015-09-27 23:58:01
PHP
Alexey, 2015-09-27 23:58:01

How to write a regular expression correctly?

Here is my code:

$table = '
<tr>
  <td valign="bottom" width="120"><p align="center">7</p></td>
  <td valign="bottom" width="20"><p align="center">40</p></td>
</tr>

<tr>
  <td valign="bottom" width="120"><p align="center">6,7</p></td>
  <td valign="bottom" width="20"><p align="center">67,9</p></td>
</tr>';

$pattern = '/<p align="center">(.+?)<\/p>/';
preg_match_all($pattern, $table, $arr);

foreach($arr as $val){
    var_dump($val);
}

It outputs all the results in one array like this:
array(4) { [0]=> string(1) "7" [1]=> string(2) "40" [2]=> string(3) "6,7" [3]=> string(4) "67,9" }

How to make each line
array(4) { 
     array(1){ [0]=> string(1) "7" [1]=> string(2) "40" }
     array(2){ [0]=> string(3) "6,7" [1]=> string(4) "67,9" }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2015-09-28
@dzheka3d

it's probably better to "sort through" 2 times
, first we pull out
, and then foreach and select the rest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question