From the line САМАРСКИЙ" — 25 отзывов need to get the number, that is, the number 25.

I do this:

 preg_match_all('#(^\-\s)([0-9])(\sотзывов$)#is', $pattern, $matches); 

returns:

 array(4) {[0]=>array(0) {}[1]=>array(0) {}[2]=>array(0) {}[3]=>array(0) {}} 

Tell me, please, where is the error?

  • Do you just need numbers in the text? \d+ . - Sasha Chernykh
  • @ SashaBlack #^(\-\s)(\d+)(\sотзывов)$#is - not working - Vladimir
  • #\s+([0-9]+)\s+отзывов#i - splash58
  • It doesn't work either - Vladimir
  • one
    I can donate a book in electronic form - to someone I offered it today already, on ozon 2000R it costs - user33274

3 answers 3

So it should turn out:

 \d+(?=\s+отзыв) 

    Try in the same browser console to do:

     /\s(\d+)\sотзыв/.exec('САМАРСКИЙ" — 25 отзывов') 

    It seems to me that your problem is in the encoding. For example, you wrote the script in CP-1251 encoding, and the site is in UTF-8 encoding (or vice versa), then regular search, of course, does not work :) Try to convert the site content to the desired encoding first.

      If you need to extract numbers from a string, then you can:

      I :

       $str = 'САМАРСКИЙ" — 25 отзывов'; preg_match_all('!\d+!', $str, $matches); print_r($matches); 

      II :

       $str = 'САМАРСКИЙ" — 25 отзывов'; $int = filter_var($str, FILTER_SANITIZE_NUMBER_INT); echo($int); 

      III :

       $str = 'САМАРСКИЙ" — 25 отзывов'; $str = preg_replace('/\D/', '', $str); echo $str; 

      UPD
      Answer after clarification:

       $str = 'САМАРСКИЙ" — 25 отзывов'; preg_match_all('#.*\s+([0-9]+)\s+отзывов#u', $str, $matches); print'<pre>';print_r($matches);print'</pre>'; 
      • The first option almost came up, but he chooses all the numbers, and I need it from this line, as I go through the code of the entire site. - Vladimir
      • one
        Add to the question that the search is not limited to the given line. - borodatych
      • Added answer. The problem was presumably in a hyphen. They are, if you do not know, several species. - borodatych
      • fiction, but finds nothing. ( - Vladimir
      • Code 1 in 1 is executed and there is no result (1) or are you trying to apply to your situation (2)? 1 . What happens on the screen? Make a conclusion of errors, maybe something will become clear there. 2 Add to the question a piece of text from where the sample goes more, the more the better. - borodatych