PHP Implode (101) With Quotes

php implode (101) with quotes

No, the way that you're doing it is just fine. implode() only takes 1-2 parameters (if you just supply an array, it joins the pieces by an empty string).

PHP Implode But Wrap Each Element In Quotes

Add the quotes into the implode call: (I'm assuming you meant implode)

$SQL = 'DELETE FROM elements
WHERE id IN ("' . implode('", "', $elements) . '")';

This produces:

DELETE FROM elements WHERE id IN ("foo", "bar", "tar", "dar")

The best way to prevent against SQL injection is to make sure your elements are properly escaped.

An easy thing to do that should work (but I haven't tested it) is to use either array_map or array_walk, and escape every parameter, like so:

$elements = array();
$elements = array_map( 'mysql_real_escape_string', $elements);

Implode with double quotes as separator

Just swap your quotes

$_words = '"'.implode('","', $array_of_words).'"';

PHP add single quotes to comma separated list

Use ' before and after implode()

$temp = array("abc","xyz");

$result = "'" . implode ( "', '", $temp ) . "'";

echo $result; // 'abc', 'xyz'

How to achieve every data has a double quotes in laravel (Implode)

Obviously your $selected_store is a simple string, so it's quite enough to apply str_replace(), like:

$converted_selected_store = '"'.str_replace(',','","',$selected_store).'"';

PHP adding single quotes to comma separated string

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 ','.

CakePHP: Escape single quotes within an implode function delimited with commas

Check out json_encode() for PHP to output a JSON string, and then look at JSON.parse() for use in your jQuery.

Instead of using implode() to generate your string, use json_encode() and then have JSON.parse() decode that for use in whatever application you need.

Edit: Added some code for clarity:

$strSuppliers = json_encode($suppliers);

and then in your jQuery:

var jsonStr = '<?php echo $suppliers; ?>';
var availableTags = JSON.parse(jsonStr);

EDIT 2: As ndm pointed out in the comments, you can do this more cleanly by directly assigning the Javascript variable to the output of json_encode():

var availableTags = <?php echo $suppliers; ?>;


Related Topics



Leave a reply



Submit