PHP如何在php中删除4个逗号后的所有值

Hi there i have a value 1,2,3,4,5,6,7 and i want to change this value into 1,2,3,4 i don't want any value after 4 in my php code,

if you have any idea how to done it with PHP or Javascript

As you provided no efforts in writing code, here's a oneliner for you to understand:

echo implode(',', array_slice(explode(',', '1,2,3,4,5,6,7', 4 + 1), 0, 4));

Fiddle here.

You can do it with preg_replace in PHP:

$string = "1,2,3,4,5,6,7";
echo preg_replace('/^(([^,]+,){3}[^,]).*$/', '$1', $string);

Output:

1,2,3,4

Similarly in Javascript:

const string = "1,2,3,4,5,6,7";
console.log(string.replace(/^(([^,]+,){3}[^,]).*$/, '$1'));

</div>