10

My variables look like this:

AAAAAAA, BB CCCCCCCC

AAAA,BBBBBB CCCCCC

I would like to remove everything before the ",",

so the results should look like:

BB CCCCCCCC

BBBBBB CCCCCC

I have worked out this to remove everything AFTER the ",":

list($xxx) = explode(',', $yyyyy);

unfortunately I dont know how to get it to work to remove everything BEFORE the ",".

kenorb
  • 137,499
  • 74
  • 643
  • 694
Andrej
  • 187
  • 2
  • 3
  • 7
  • You might find [`s($str)->afterFirst(',')`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L435) helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). – caw Jul 27 '16 at 03:55
  • `$xxx = explode(',', $yyyyy, 2)[1];` – kenorb Nov 21 '18 at 13:29

6 Answers6

27

Since this is a simple string manipulation, you can use the following to remove all characters before the first comma:

$string = preg_replace('/^[^,]*,\s*/', '', $input);

preg_replace() allows you to replace parts of a string based on a regular expression. Let's take a look at the regular expression.

  • / is the start delimiter
    • ^ is the "start of string" anchor
    • [^,] every character that isn't a comma (^ negates the class here)
      • * repeated zero or more times
    • , regular comma
    • \s any whitespace character
      • * repeated zero or more times
  • / end delimiter
Andrew Moore
  • 90,677
  • 30
  • 161
  • 174
20

I wouldn't recommend using explode, as it causes more issues if there is more than one comma.

// removes everything before the first ,
$new_str = substr($str, ($pos = strpos($str, ',')) !== false ? $pos + 1 : 0);

Edit:

if(($pos = strpos($str, ',')) !== false)
{
   $new_str = substr($str, $pos + 1);
}
else
{
   $new_str = get_last_word($str);
}
Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
2
list(,$xxx) = explode(',', $yyyyy, 2);
nickf
  • 520,029
  • 197
  • 633
  • 717
2

try this it gets the last stuff after the , if no , is present it will check from the last space, i wrapped it in a function to make it easy:

<?php 
$value='AAAA BBBBBB CCCCCC';
function checkstr($value){
    if(strpos($value,',')==FALSE){
        return trim(substr(strrchr($value, ' '), 1 ));  
    }else{
        return trim(substr($value, strpos($value,',')),',');
    }
}

echo checkstr($value);
?>
Lawrence Cherone
  • 44,769
  • 7
  • 56
  • 100
  • this works good too! but how can i tell the script to use the last word of the string in case there is no "," ? – Andrej Mar 16 '11 at 18:35
1

you can do:

$arr = explode(',', $yyyyy);
unset($arr[0]);
echo implode($arr);
Naftali
  • 142,114
  • 39
  • 237
  • 299
  • I think this is nice. Though you need to echo implode(',',$arr); to not lose the other commas. – embe Dec 11 '14 at 10:37
  • @embe there are no other commas in the question's example – Naftali Dec 11 '14 at 10:56
  • True. It was in my case, and to answer "Remove Everything Before the first “,” in a string" I think it's good to add. Thank you for a good solution. – embe Dec 11 '14 at 11:11
0

Regex is generally expensive and i wouldn't recommend it for something as simple as this. Using explode and limiting it to 2 will probably result in the same execution time as using str_pos but you wouldn't have to do anything else to generate the required string as its stored in the second index.

 //simple answer 
 $str = explode(',', $yyyyy,2)[1]; 

OR

//better 

$arr = explode(',', $yyyyy,2);
$str = isset($arr[1]) ? $arr[1] : '';
TarranJones
  • 3,817
  • 1
  • 34
  • 51