How to truncate a delimited string in PHP?

Solution:1

Use explode() to separate the elements into an array, then you can slice off the first 10, and implode() them to create the new string.

$arr = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";
$a = explode ('|',$arr);
$b = array_slice($a,0,10);
$c = implode('|', $b);

Solution:2

I would use explode to separate the string into an array then echo the first ten results like this

$string = "5|2288|502|4208|55|23217|235|10|3845|19053|1885|61|324|9004";

$arr = explode("|", $string);

for($i = 0; $i < 10; $i++){
    echo $arr[$i];
}