-2
$A=123132,32132,123132321321,3
$B=1,32,99
$C=456,98,89
$D=1

I want to cut string after first comma

output. . .

$A=123132
$B=1
$C=456
$D=1
rizzz86
  • 3,762
  • 8
  • 33
  • 51
  • php.net provides a nice reference documentation: http://www.php.net/manual/en/function.explode.php – Joop Eggen Jun 01 '14 at 08:13
  • possible duplicate of [Get first value of comma separated string](http://stackoverflow.com/questions/10540928/get-first-value-of-comma-separated-string) – revo Jun 01 '14 at 08:14

4 Answers4

1

You can do this with strpos to get the position of the first comma, and substr to get the value prior to it:

<?php

    $val='123132,32132,123132321321,3';
    $a=substr($val,0,strpos($val,','));

    echo $a;

?>

Output:

123132

Fluffeh
  • 32,630
  • 16
  • 65
  • 80
1

$newA = current(explode(",", $A));

from: PHP substring extraction. Get the string before the first '/' or the whole string

Community
  • 1
  • 1
Kinnectus
  • 869
  • 6
  • 14
0

There are a number of ways to accomplish this:

substr($A,0,strpos($A,","))

or

current(explode(",", $A))

will both return the value 123132

mifi79
  • 1,076
  • 6
  • 8
-1

You can do

$A='123132,32132,123132321321,3';
$t=explode(',',$A);
$result = $t[0];
Michel
  • 3,874
  • 4
  • 35
  • 49