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
  • 155,785
  • 88
  • 678
  • 743
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
  • 93,497
  • 30
  • 163
  • 175
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);
}
  • this is the one you should use, IMHO – Pedro Mar 16 '11 at 18:22
  • +1 -- It's faster and more readable (IMHO) than the regexp. Go with this. – Martin Tournoij Mar 16 '11 at 18:24
  • @Carpetsmoker: This fails however when there isn't any comma. `strpos()` returns false when there isn't a comma, `false + 1` = 1 which means you'll end up stripping your first character. – Andrew Moore Mar 16 '11 at 18:25
  • @Andrew: Fixed so it doesn't fail if there is no comma present. I do prefer your regex solution over this. –  Mar 16 '11 at 18:27
  • Yes, this worked great! One last thing: is it possible to include a "if" statement that, in case there is no ",", will use another function to use the last word of the string? – Andrej Mar 16 '11 at 18:28
  • 1
    use trim() like my example below – Lawrence Cherone Mar 16 '11 at 18:28
  • @Andrej: Do you need assistance with getting the last word in the string? –  Mar 16 '11 at 18:36
  • @Tim: I have a function already that outputs the last word of a given string. I have problems to tell the script to use that function in case there is NO ",". If thats even possible. – Andrej Mar 16 '11 at 18:38
  • p.s. i think you forgot a ")" after "if(($pos = strpos($str, ',')" it works great with "if(($pos = strpos($str, ','))" – Andrej Mar 16 '11 at 18:43
  • check mine below for better-ness ;P what is "get_last_word" – Lawrence Cherone Mar 16 '11 at 19:02
2
list(,$xxx) = explode(',', $yyyyy, 2);
nickf
  • 537,072
  • 198
  • 649
  • 721
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
  • 46,049
  • 7
  • 62
  • 106
  • 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
  • 144,921
  • 39
  • 244
  • 303
  • 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
  • 4,084
  • 2
  • 38
  • 55