[ACCEPTED]-Remove everything from first occurrence of a character to the end of a string in php-extract

Accepted answer
Score: 36

Here's one way:

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


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


$print = explode(',', $print)[0];


Score: 12

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


Score: 6

You could use a regular expression, but 2 if it's always going to be a single pairing 1 with a comma, I'd just do this:

$printArray = explode(",", $print);
$print = $printArray[0];
Score: 5

You can also use current function:

$firstpart = current(explode(',', $print)); // will return current item in array, by default first

Also other functions 1 from this family:

$nextpart = next(explode(',', $print)); // will return next item in array

$lastpart = end(explode(',', $print)); // will return last item in array
Score: 3
$string="50 days,7 hours";  
$s = preg_split("/,/",$string);
print $s[0];


Score: 1

If you are going to use strstr() then you need to 32 be 100% sure that a comma will exist in 31 the string or be prepared to handle a false return 30 value. Set its 3rd parameter as true to access 29 all characters before the first occurring 28 comma. If there is no comma, then the return 27 value will be false.

preg_replace() will likely be the least 26 efficient, but it is a single function call 25 and the pattern that I'll use will not mutate 24 the string if a comma is not found. If 23 your input string might have newline characters 22 in it, use the s pattern modifier to allow 21 the dot (.) to match these as well.

strtok() is a 20 concise tool to use, but I find its name 19 to be less expressive / more cryptic than 18 other functions (maybe this is my own personal 17 bias toward strstr(). This may confuse or slow 16 down future readers of your code. This 15 function will return false if the input string 14 has no length.

If you are going to use explode(), then 13 don't ask php to perform more than 2 explosions. The 12 good thing about this function is that if 11 the comma doesn't exist, then it will return 10 the whole string. I prefer not to use explode() because 9 it generates an array of which the first 8 element is accessed -- I prefer to not generate 7 more data than I need.

sscanf() is a little too clumsy 6 for this task since a negated character 5 class needs to be used as well as the null 4 coalescing operator. If the delimiter was 3 a space, then %s could be used, but the null 2 coalescing operator would still be necessary 1 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,7 hours" false false
preg_replace('/,.*/', '', $print) "50 days,7 hours" "50 days" ""
strtok($print, ",") "50 days,7 hours" "50 days" false
explode(',', $print, 2)[0] "50 days,7 hours" "50 days" ""
sscanf($print, '%[^,]')[0] ?? $print "50 days,7 hours" "50 days" ""

More Related questions