tags:

views:

317

answers:

6
+1  Q: 

PHP str_replace

I'm currently using str_replace to remove a usrID and the 'comma' immediately after it:

For example:

$usrID = 23;
$string = "22,23,24,25";
$receivers = str_replace($usrID.",", '', $string);  //Would output: "22,24,25"

However, I've noticed that if:

$usrID = 25; //or the Last Number in the $string

It does not work, because there is not a trailing 'comma' after the '25'

Is there a better way I can be removing a specific number from the string?

Thanks.

A: 

Try using preg:

<?php
$string = "22,23,24,25";
$usrID = '23';
$pattern = '/\b' . $usrID . '\b,?/i';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>

Update: changed $pattern = '/$usrID,?/i'; to $pattern = '/' . $usrID . ',?/i'; Update2: changed $pattern = '/' . $usrID . ',?/i to $pattern = '/\b' . $usrID . '\b,?/i' to address onnodb's comment...

beggs
That pattern is going to change "14,150,233" with $usrID = 23 into "14,150," --- that's not correct.
onnodb
+2  A: 

Another issue is if you have a user 5 and try to remove them, you'd turn 15 into 1, 25 into 2, etc. So you'd have to check for a comma on both sides.

If you want to have a delimited string like that, I'd put a comma on both ends of both the search and the list, though it'd be inefficient if it gets very long.

An example would be:

$receivers = substr(str_replace(','.$usrID.',', ',', ','.$string.','),1,-1);
David
+2  A: 

YOu could explode the string into an array :

$list = explode(',', $string);
var_dump($list);

Which will give you :

array
  0 => string '22' (length=2)
  1 => string '23' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

Then, do whatever you want on that array ; like remove the entry you don't want anymore :

foreach ($list as $key => $value) {
    if ($value == $usrID) {
        unset($list[$key]);
    }
}
var_dump($list);

Which gives you :

array
  0 => string '22' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

And, finally, put the pieces back together :

$new_string = implode(',', $list);
var_dump($new_string);

And you get what you wanted :

string '22,24,25' (length=8)

Maybe not as "simple" as a regex ; but the day you'll need to do more with your elements (or the day your elements are more complicated than just plain numbers), that'll still work :-)


EDIT : and if you want to remove "empty" values, like when there are two comma, you just have to modifiy the condition, a bit like this :

foreach ($list as $key => $value) {
    if ($value == $usrID || trim($value)==='') {
        unset($list[$key]);
    }
}

ie, exclude the $values that are empty. The "trim" is used so $string = "22,23, ,24,25"; can also be dealt with, btw.

Pascal MARTIN
This is what I was thinking of, which helped very much. +1 for Daryl as well, which I may use for something else at one point. Thanks again.
Dodinas
You're welcome ; Have fun !
Pascal MARTIN
+2  A: 

An option similar to Pascal's, although I think a bit simipler:

$usrID = 23;
$string = "22,23,24,25";
$list = explode(',', $string);
$foundKey = array_search($usrID, $list);
if ($foundKey !== false) {
    // the user id has been found, so remove it and implode the string
    unset($list[$foundKey]);
    $receivers = implode(',', $list);
} else {
    // the user id was not found, so the original string is complete
    $receivers = $string;
}

Basically, convert the string into an array, find the user ID, if it exists, unset it and then implode the array again.

Darryl Hein
A: 

Simple way (providing all 2 digit numbers):

$string = str_replace($userId, ',', $string);
$string = str_replace(',,','', $string);
rojoca
A: 

I would go the simple way: add commas around your list, replace ",23," with a single comma then remove extra commas. Fast and simple.

$usrID = 23;
$string = "22,23,24,25";
$receivers = trim(str_replace(",$usrID,", ',', ",$string,"), ',');

With that said, manipulating values in a comma separated list is usually sign of a bad design. Those values should be in an array instead.

Josh Davis