how to add single quotes in a string - php

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.

Related

PHP adding single quotes to comma separated string

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) . "'";

PHP Convert a string of comma separated values into another format without using a loop

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'

split array inside single quotes without using loop

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/

How to put a variable in quotes?

I have a variable $teste = hiiiii
I need to put the variable inside a quote and a double quote.
Like this:
"'$teste'" for the value be this "'hiiiii'"
I'm trying concatenation but not working. How do that???
You can escape your quotes:
echo "\"'$teste'\"";
will print out: "'hiiiii'"
You can do it like this:
$result = '"\'' . $teste . '\'"';
this will result in: "'hiiiii'".
Demo
Try before buy
Using basic cheat of single and double quote
$test = 'hi';
echo '"'."'".$test."'".'"';
echo "'".'"'.$test.'"'."'";

Add quotes to values in a string separated by a comma php

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

Categories