-2

I've a following regular expression

[^|]+(|[^|]+){15}

for the following line

|     1 |  2567 | Алма-атинська(І) вул.                     | 2     | 2      |5678     |Типовий лічил-|Санвузол          | 15.12.2016 | 10.11.2016 | 10.11.2020 | 23.10.2017 |            |    43.00 |bot       |

Its work fine, and I got all text between '|', but how can I pass any strings for these matches? For example, trim all backspaces, or delete some subsring.

Neil
  • 14,063
  • 3
  • 30
  • 51
NisuSan
  • 197
  • 1
  • 13

1 Answers1

0

You can explode(), followed by trim() to remove additional spaces, etc. And finally use array_filter to remove blank values from the array:

$str = '|     1 |  2567 | Алма-атинська(І) вул.                     | 2     | 2      |5678     |Типовий лічил-|Санвузол          | 15.12.2016 | 10.11.2016 | 10.11.2020 | 23.10.2017 |            |    43.00 |bot       |';
$arr = array_filter(array_map(function($value){return trim($value);},explode('|', $str)));
print_r($arr);

Output:

Array ( [1] => 1 [2] => 2567 [3] => Алма-атинська(І) вул. [4] => 2 [5] => 2 [6] => 5678 [7] => Типовий лічил- [8] => Санвузол [9] => 15.12.2016 [10] => 10.11.2016 [11] => 10.11.2020 [12] => 23.10.2017 [14] => 43.00 [15] => bot )
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • But if string not contains this pattern, how can I escape it? For example this line: `$str = "+-------+--------------+-------------------------------------------+-------+--------+--------------+--------------+------------------+----------------------------------------------------------------+----------+-----------------+"; ` – NisuSan Nov 15 '17 at 10:34
  • the question you asked talks about spaces. this is a completely different string, having no spaces at all, and infact have + and - signs. – Milan Chheda Nov 15 '17 at 10:51