Remove an item from a comma-separated string [duplicate] - php

This question already has answers here:
Fastest way of deleting a value in a comma separated list
(4 answers)
Closed 2 years ago.
Let's say I have a string:
cat,mouse,dog,horse
Is there a regex or a function that will work as follows?
1)"cat" return string ->"mouse,dog,horse"
2)"mouse" return string ->"cat,dog,horse"
3)"dog" return string ->"cat,mouse,horse"
4)"horse" return string ->"cat,mouse,dog"
I need to eliminate the selected item from the string and return the remaining parts of the string.

You mean a function that removes a certain element? Try this:
function removeFromString($str, $item) {
$parts = explode(',', $str);
while(($i = array_search($item, $parts)) !== false) {
unset($parts[$i]);
}
return implode(',', $parts);
}
Demo

It's as simple as exploding the string (str_getcsv) and then removing the searched term. If you have an array, then array_diff makes it very simple:
return array_diff(str_getcsv($list), array($search));

Working demo.
This converts both string inputs to arrays, using explode() for the list. Then you just do array_diff() to output what's in the second array but not the first. Finally we implode() everything back into CSV format.
$input = 'cat';
$list = 'cat,mouse,dog,horse';
$array1 = Array($input);
$array2 = explode(',', $list);
$array3 = array_diff($array2, $array1);
$output = implode(',', $array3);

Related

How to split string into alphabetic and numeric components? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 6 years ago.
How do I split a string by . delimiter in PHP? For example, if I have the string "a.b", how do I get "a"?
explode does the job:
$parts = explode('.', $string);
You can also directly fetch parts of the result into variables:
list($part1, $part2) = explode('.', $string);
explode('.', $string)
If you know your string has a fixed number of components you could use something like
list($a, $b) = explode('.', 'object.attribute');
echo $a;
echo $b;
Prints:
object
attribute
$string_val = 'a.b';
$parts = explode('.', $string_val);
print_r($parts);
Documentation: explode
The following will return you the "a" letter:
$a = array_shift(explode('.', 'a.b'));
Use:
explode('.', 'a.b');
explode
$array = explode('.',$string);
Returns an array of split elements.
To explode with '.', use:
explode('\\.', 'a.b');

how to create simple array(without key) by putting available values in php [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have values like
"34,37"
and I want to create array using this values, array like
array('34','37')
How to create such array if I have values.
hope this will help you :
you can use explode function if you have values as string;
$string = '34,37';
$data = explode(',',$string):
print_r($data); /*output array*/
for more : http://php.net/manual/en/function.explode.php
If you have a string like this:
$str = "1,2,3,4,5,6";
And you want to convert it into array, just use explode()
$myArray = explode(',', $str);
var_dump($myArray);
Have a look here for more information
As per my comment, you should use preg_split function. for more details about preg_split function please read PHP manual and also you can use explode function Explode function PHP manual
<?php
$string = '34,37';
$keywords = preg_split("/[\s,]+/", $string);
//OR $keywords = preg_split("/,/", $string); separated by comma only
print_r($keywords);
you can check your desired Output here
Try this,
$val = "34,37"
$val = explode(',', $val);
print_r($val);
output of above array:
Array
(
[0] => 34,
[1] => 37
)

In comma delimited string is it possible to say "exists" in php

In a comma delimited string, in php, as such: "1,2,3,4,4,4,5" is it possible to say:
if(!/*4 is in string bla*/){
// add it via the .=
}else{
// do something
}
In arrays you can do in_array(); but this isn't a set of arrays and I don't want to have to convert it to an array ....
Try exploding it into an array before searching:
$str = "1,2,3,4,4,4,5";
$exploded = explode(",", $str);
if(in_array($number, $exploded)){
echo 'In array!';
}
You can also replace numbers and modify the array before "sticking it back together" with implode:
$strAgain = implode(",", $exploded);
You could do this with regex:
$re = '/(^|,)' + preg_quote($your_number) + '(,|$)/';
if(preg_match($re, $your_string)) {
// ...
}
But that's not exactly the clearest of code; someone else (or even yourself, months later) who had to maintain the code would probably not appreciate having something that's hard to follow. Having it actually be an array would be clearer and more maintainable:
$values = explode(',', $your_string);
if(in_array((str)$number, $values)) {
// ...
}
If you need to turn the array into a string again, you can always use implode():
$new_string = implode(',', $values);

How do I place a comma between each character in a string with PHP? [duplicate]

hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);

PHP: Split string [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 6 years ago.
How do I split a string by . delimiter in PHP? For example, if I have the string "a.b", how do I get "a"?
explode does the job:
$parts = explode('.', $string);
You can also directly fetch parts of the result into variables:
list($part1, $part2) = explode('.', $string);
explode('.', $string)
If you know your string has a fixed number of components you could use something like
list($a, $b) = explode('.', 'object.attribute');
echo $a;
echo $b;
Prints:
object
attribute
$string_val = 'a.b';
$parts = explode('.', $string_val);
print_r($parts);
Documentation: explode
The following will return you the "a" letter:
$a = array_shift(explode('.', 'a.b'));
Use:
explode('.', 'a.b');
explode
$array = explode('.',$string);
Returns an array of split elements.
To explode with '.', use:
explode('\\.', 'a.b');

Categories