You can get the $first
and $second
using this one line regex code
$data = "m-12,s-52,s-52";
$beforefirstcomma = preg_replace("/,(.+)/", "", $data);
$afterlastcomma = preg_replace("/(.+),/", "", $data);
$afterfirstcomma = preg_replace("/^[^,]+,+/", "", $data);
// outputs
echo $beforefirstcomma; // m-12
echo "<br>";
echo $afterlastcomma; // s-52
echo "<br>";
echo $afterfirstcomma; // s-52,s-52
Final Note
I still recommend using explode()
because it is more efficient and performant than running preg_replace()
multiple times on the $data
string.