V
V
Vlad Saenko2020-08-02 10:26:07
PHP
Vlad Saenko, 2020-08-02 10:26:07

What am I doing wrong with file_get_contents?

<?php
$str=file_get_contents('*******page=1');
preg_match_all('#<a class="item last" href="/rus/list/avtoservis\?page=(.+?)#su', $str, $pages);
dd($pages);

the tag itself
<a class="item last" href="/rus/list/avtoservis?page=5">последняя »</a>

I need to pull out the number five, but as a result I get two numbered arrays. The required figure is present there, but I do not understand where the rest of the data comes from.

array:2 [▼
  0 => array:2 [▼
    0 => "<a class="item last" href="/rus/list/avtoservis?page=5"
    1 => "<a class="item last" href="/rus/list/avtoservis?page=5"
  ]
  1 => array:2 [▼
    0 => "5"
    1 => "5"
  ]
]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-08-02
@Domovik93

There are probably two pagination blocks on the page that match the specified regular expression.
By default, the PREG_PATTERN_ORDER flag is passed to preg_match_all

PREG_PATTERN_ORDER Orders
the results so that the $matches[0] element contains an array of full pattern occurrences, the $matches[1] element contains an array of occurrences of the first subpattern, and so on.

Therefore, $pages[0] contains all occurrences of the pattern, and $pages[1] contains occurrences of the first subpattern (the page number in your case).

V
Vladislav, 2020-08-02
@cr1gger

0 array it is always there all found string
1 what you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question