JavaJava21
JavaJava21

Reputation: 61

Get number in between letter and dash(-) php

I have a string that contains a letter, numbers and a dash (-). The numbers represent a specific month and a specific year.

(i.e. 'M4-16' -- April 2016)

I need to break out the 4 by itself and then the 16 by itself. I'm blanking on how to do this right now, so sorry for not including what I've tried.

Thank you in advance!

Upvotes: 1

Views: 375

Answers (1)

Matthew Herbst
Matthew Herbst

Reputation: 31983

Since you know the format of the relatively simple string, no regex is needed:

$str = "M4-16";
$parts = explode("-", $str); // Gives ["M4", "16"]
$part1 = substr($parts[0], 1); // Gives 4
$part2 = $parts[1]; // Gives 16

Upvotes: 3

Related Questions