Good day!

PHP question. There is a line: 7 x 17 ET50, in which the values ​​of the figures may vary. I need to find a function that would cut, for example, the numbers after the "x" and before the "ET". I tried this: $d5[0] = substr($d5[0], strpos($d5[0], 'x'), 4); But this design cuts off the digits TOGETHER with X, but I do not need X.

  • 2
    What should you get from the 7 х 17 ET50 line? And you should probably look in the direction of regular expressions. - Visman
  • How to explain this)) There the site is ready, but they are asked to make these numbers come from the database (they are written in such a line), then each of these numbers is cut into a link, then when you click on the main page, a separate page is created in which all the values ​​from the database that matched the selected car would be shown, but these numbers in the form of links were clicked on a different page when clicked, and already there disks with a given parameter were shown automatically from the database. In general, tin)) There, almost everything has been done (I figured out the tires), you just need to cut this line. - Artur Mark
  • I already have stropists, strps, and strpos — that I haven’t tried. Either he takes with X, or after X at once, or does not work out at all normally. - Artur Mark
  • and what, stupidly strpos (...) + 1 does not help? - Mike
  • Did not try to look aside php.su/functions/?cat=pcre - is it easy to do there? - Andrewus

1 answer 1

This is how your problem is solved:

 $str = '7 х 17 ET50'; $pattern = "/х (\d+) ET/i"; preg_match($pattern, $str , $matches); print_r($matches); 

Here only numbers are selected.

  • Thank you very much for this decision, but the user under the nickname Mike suggested simply adding "+1" behind the page line bracket)) Everything was so simple - Artur Mark
  • I just noticed that in the case of a 4-digit number, for example, your structure will break (since you no longer fit 4 characters from the "x"). It will be necessary to consider the location of the substring "ET", and this makes the code so cumbersome and complex that it is better to do it right away. - Andrewus
  • And there will not be a 4-digit number. Even 3-digit will not. There are only 2-digit ones, because these are the values ​​of the diameter of the wheel of the car, and the disks are more than 100 inches hard to imagine)) Therefore, the code is obtained according to the logic "cut everything to X, then after X into 2 values ​​and cut everything after ET". Just in the end, your option will be much more cumbersome, because This solution fits into 1 line, and in your case it is necessary 3. But anyway, thanks, I will remember this method for the future - Artur Mark
  • Well, something is not worth doing in php, so it is to minimize the number of lines :) Wheels for ATVs and tractors this script in the future will not have to handle? - Andrewus