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
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];
You can also use current function:
$firstpart = current(explode(',', $print)); // will return current item in array, by default first
Also other functions from this family:
$nextpart = next(explode(',', $print)); // will return next item in array
$lastpart = end(explode(',', $print)); // will return last item in array
$string="50 days,7 hours";
$s = preg_split("/,/",$string);
print $s[0];
If you are going to use strstr()
then you need to be 100% sure that a comma will exist in the string or be prepared to handle a false
return value. Set its 3rd parameter as true
to access all characters before the first occurring comma. If there is no comma, then the return value will be false
.
preg_replace()
will likely be the least efficient, but it is a single function call and the pattern that I'll use will not mutate the string if a comma is not found. If your input string might have newline characters in it, use the s
pattern modifier to allow the dot (.
) to match these as well.
strtok()
is a concise tool to use, but I find its name to be less expressive / more cryptic than other functions (maybe this is my own personal bias toward strstr()
. This may confuse or slow down future readers of your code. This function will return false
if the input string has no length.
If you are going to use explode()
, then don't ask php to perform more than 1 explosion. The good thing about this function is that if the comma doesn't exist, then it will return the whole string. I prefer not to use explode()
because it generates an array from which the first element is accessed -- I prefer to not generate more data than I need.
sscanf()
is a little too clumsy for this task since a negated character class needs to be used as well as the null coalescing operator. If the delimiter was a space, then %s
could be used, but the null coalescing operator would still be necessary because sscanf()
will not make zero-length matches.
Code: (Demo) (Demo if no comma) (Demo if string empty)
"50 days,7 hours" | "50 days" | "" | |
---|---|---|---|
strstr($print, ',', true) |
"50 days" | false | false |
preg_replace('/,.*/', '', $print) |
"50 days" | "50 days" | "" |
strtok($print, ",") |
"50 days" | "50 days" | false |
explode(',', $print, 2)[0] |
"50 days" | "50 days" | "" |
sscanf($print, '%[^,]')[0] ?? $print |
"50 days" | "50 days" | "" |