I want to remove everything (including the comma) from the first comma of a string in PHP.
For example,
$print = "50 days,7 hours";
should become
50 days
Here's one way:
$print = preg_replace('/^([^,]*).*$/', '$1', $print);
Another:
list($print) = explode(',', $print);
Or:
$print = explode(',', $print)[0];
This should work for you:
$r = (strstr($print, ',') ? substr($print, 0, strpos($print, ',')) : $print);
# $r contains everything before the comma, and the entire string if no comma is present
You could use a regular expression, but if it's always going to be a single pairing with a comma, I'd just do this:
$printArray = explode(",", $print);
$print = $printArray[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With