Skip to content
Advertisement

Remove everything from first occurrence of a character to the end of a string in php

I want to remove everything (including the comma) from the first comma of a string in php. eg.

$print = "50 days,7 hours";

should become

50 days

Advertisement

Answer

Here’s one way:

$print = preg_replace('/^([^,]*).*$/', '$1', $print);

Another:

list($print) = explode(',', $print);

Or:

$print = explode(',', $print)[0];
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement