I know I have done this before. I am not sure why I am struggling to do something so simple.
All I am trying to do is add a single quote to a comma separated string.
The php process initially looks like this:
<?php
$checknumber = mysqli_real_escape_string($dbc,trim($_POST['checknumber']));
$splitnumber = preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$checknumber )));
$numbers = "'" . implode("', '", $splitnumber ) ."'";
echo $checknumber;
echo $splitnumber;
echo $numbers;
?>
The results look like this:
// $checknumber
AMD111111,AMD222222,AMD333333
// $splitnumber
AMD111111,AMD222222,AMD333333
// $numbers
''
I need the results of $numbers to look like this:
'AMD111111','AMD222222','AMD333333'
The funny thing is I am using a piece of code I've used before that works. So I am at a loss as to why the code is not working here.
The implode() function requires an array as second argument.
It looks like your $splitnumber is just a simple string not an array of strings as it should probably be.
Try splitting your $splitnumber by commas using the explode() function and put the result of that in the implode function.
Should look like that (not tested):
$splitnumber = ...;
$splittedNumbers = explode(",", $splitnumber);
$numbers = "'" . implode("', '", $splittedNumbers) ."'";
There is probably a cleaner solution by just replacing all occurences of , with ','.
Your implode isn't setup correctly.
$numbers = "'" . implode("','", $splitnumber) . "'";
Related
site_pages:
$site_pages = 'groups_permissions.php,b.php,c.php,d.php';
values to replace:
$id = 'groups_permissions.php,b.php,c.php,d.php';
expected result:
should be empty
I am using the following to create array using the above comma separated list:
// THIS WILL CREAET ARRAY LIKE 'groups_permissions.php','b.php','c.php','d.php'
$egu_explode = explode(",", $id);
$editgroup_users_array = "'" . implode ( "', '", $egu_explode ) . "'";
// REPLACE FROM COLUMN
$site_pages = $db_con->query("SELECT site_pages FROM settings LIMIT 1")->fetchColumn();
$remove_page = str_replace([$editgroup_users_array], '', $site_pages);
$remove_commas = preg_replace("/,+/", ",", $remove_page);
$updated_pages = trim($remove_commas, ",");
echo $updated_pages;
PROBLEM:
The above code does not replace any string and gives values from site_pagescolumn but if I place the array('groups_permissions.php','b.php','c.php','d.php') manually in str_replace then it replaces the strings perfectly.
Change
$editgroup_users_array = "'" . implode ( "', '", $egu_explode ) . "'";
to
$editgroup_users_array = implode(",", $egu_explode);
AND
$remove_page = str_replace([$editgroup_users_array], '', $site_pages);
to
$remove_page = str_replace($editgroup_users_array, '', $site_pages);
UPDATE #1
Better to directly do,
$remove_page = str_replace($id,'', $site_pages);
UPDATE #2
If you want to replace groups_permissions.php,b.php,c.php,d.php regardless of their order, pass them in an array as shown below.
$remove_page = str_replace(array('groups_permissions.php','b.php','c.php','d.php'),'',$site_pages);
Or, in your case, you can better do like this-
$remove_page = str_replace($egu_explode,'',$site_pages);
If you are using a variable like this [$editgroup_users_array] then it will consider it an array and it will give like the following output.
Array
(
[0] => 'groups_permissions.php', 'b.php', 'c.php', 'd.php'
)
and if you are using it as directly like this :
str_replace(['groups_permissions.php', 'b.php', 'c.php', 'd.php'],'',$site_pages);
It will work.
So, your str_replace function should not use [] as it will convert a string to an array.
So you should simply remove brackets [] and use this.
$remove_page = str_replace($editgroup_users_array, '', $site_pages);
OR
As #vivek_23 said, you should simply use this:
$remove_page = str_replace($id,'', $site_pages);
I am using the following code for adding the singles quotes for a string
$gpids=implode("','",array_unique($groupIds));
My output coming like these
156','155','161','151','162','163
I want my output like these
'156','155','161','151','162','163'
please help me
Using concatenation operator
$gpids = "'".implode("','",array_unique($groupIds))."'";
Just concate quote :
<?php
$gpids="'" . implode("','",array_unique($groupIds)) . "'";
echo $gpids;
?>
You have two options:
Simple one:
$string = "'" . implode("','",array_unique($groupIds)) . "'";
Second one:
function add_quotes($str) {
return sprintf("'%s'", $str);
}
$string = implode(',', array_map('add_quotes', array_unique($groupIds)));
Try this
$query=$key.'='."'$value'".',';
Here $value will have single quotes.
In php, if I had a string of comma separated data like this which came from a database:
John,Paul,Ringo,George
how could I convert it to something like this, without using a for loop or any extra processing. Does php have a function that can do this?
$str = 'John','Paul','Ringo','George';
I currently split the string using explode and then reformat it. Anyway to achieve this without doing that?
The formatted string is sent to a SQL where it's used in a MySQL IN() function.
If you absolutely don't want to use explode() you could use
$str = 'John,Paul,Ringo,George';
$newStr = "'" . str_replace(",","','", $str) . "'";
You can use preg_replace with $1 thing.
UPDATED:
See usage example:
echo preg_replace('((\\w+)(,?))', '"$1"$2', 'John,Paul,Ringo,George');
you can use explode like below
$db_data = 'John,Paul,Ringo,George';//from your db
$l = explode(',',$db_data);
echo "select * from table where column IN('".implode("','",$l)."')";
output:
select * from table where column IN('John','Paul','Ringo','George')
You can use the explode and implode functions in PHP.
$names = explode(',', 'John,Paul,Ringo,George');
echo implode(',', $names);
I hope I got you right:
$str_1 = 'John,Paul,Ringo,George';
$str_2 = explode(',', $str_1);
$str_3 = implode('\',\'', $str_2);
$str_4 = '\''.$str_3.'\'';
echo $str_4;
Output: 'John','Paul','Ringo','George'
$l = 'John,Paul,Ringo,George';
$lx = "'" . implode("','", explode(",", $l)) . "'";
echo $lx; // 'John','Paul','Ringo','George'
I have array of Ids (123456,654789,395855). I will need to split it with single qoute and comma to be able to use it in mysql query. In fact I need this for IN() function.
Is there any native php function that puts all array nodes inside quotes (retrieve string ) without using any loop?
so far I come up with this. but I am looking at better way.
$ids = array(123456,654789,395855);
foreach ($ids as $id){
$stringIds .= "'". $id . "',";
}
$stringIds = rtrim ($stringIds, ",");
debug($stringIds);
Just use implode() as usual, with some extra quotes before/after:
$ids = array(123456,654789,395855);
$string = "'" . implode("','", $ids) . "'";
No, there is not a native function that does this. What you have will work just fine.
You can use implode and array_map to achieve this:
implode(', ', array_map(function($id) { return "'$id'"; }, $ids));
You can use array_map and implode you may also look into array_reduce
$ids = array(123456,654789,395855);
$quoted = array_map(function($id) { return "'$id'"; }, $ids);
$stringIds = implode(', ', $quoted);
echo $stringIds;
See the ideone
You can separate an array into single quotes and comma separated list using php's implode function.
$array = array(123456,654789,395855);
$comma_list = "'" .implode("', '", $array) . "'";
echo $comma_list;
Here is reference to an article.
http://codingcyber.com/how-split-array-single-quotes-and-comma-separated-list-php-57/
I have a search box that can contain multiple values using a comma, eg Pasta, tuna, eggs
Im using FULLTEXT mysql search but I need to use some kind of preg_replace to turn Pasta, tuna, eggs into 'Pasta','tuna','eggs'
If I enter this 'Pasta','tuna','eggs' into the search box the results are correct.
Don't use regular expressions for problems that can be solved otherwise. What you want is a simple string replacement:
$string = "'" . str_replace(",", "','", $string) . "'";
You should escape quotes inside the string first, though (don't know what your escape character is, assuming it's backslash):
$string = "'" . str_replace(array("'", ","), array("\\'", "','"), $string) . "'";
Are you building an SQL query with the list? If so, you should take some time to make sure the resulting SQL is properly escaped as well.
$myList = "pasta, tuna, eggs";
$items = preg_split("/[,\\s]+/", $myList);
$sqlItems = array();
foreach ($items as $item) {
$sqlItems[] = "'" . mysql_real_escape_string($item) . "'";
}
// Add new list to SQL
$sql .= implode(",", $sqlItems);
Do you have any comma in values?
If not you could use something like:
preg_replace('/[^,]+/g', '\'\1\'', preg_replace('/\'/g', '\\\'', $text))
implode your string, then foreach resulting array and add needed symbols
Guys sorry for the trouble but I've solved my own question! Ive looked at this and it was all wrong to begin with.
I had to replace each , with a space and a plus sign so ", tuna" = " +tuna"
Thanks anyway