PHP Implode wrap in tags
In this way you are wrapping the entire set in one span, you have to add the closing/opening tag to the implode:
$value = "<span>".implode('</span>,<span>', $values)."</span>";
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);
wrapping href around implode variable
You could seriously abuse implode
in a manner similar to what you have tried yourself and make it work, but that's really not a good idea.
What you want is to move from a list of URLs to a list of anchor tags, which is possible with array_map
:
if ($website = get_post_meta($post->ID, 'website')) {
$links = array_map(
function($url) {
$url = htmlspecialchars($url);
return sprintf ('<a href="http://%s">%s</a>', $url, $url);
},
$website);
echo implode(', ', $links);
}
Implode and Explode Array then wrap it inside a sample data
$sample = "Col1,Col2,Col3";
$test= explode(',',$sample);
$_test = '';
foreach($test as $t){
$_test .= "FORMAT(SUM('$t'),2),";
}
$_test = rtrim($_test,',');
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, add a newline with implode
I suspect it is because you are echoing the data to the browser and it's not showing the line break as you expect. If you wrap your implode in the the <pre>
tags, you can see it is working properly.
Additionally, your arguments are backwards on your implode function, according to current documentation. However, for historical reasons, parameters can be in either order.
$array = array('this','is','an','array');
echo "<pre>".implode(",\n",$array)."</pre>";
Output:
this,
is,
an,
array
Related Topics
Seamless Way to Check If User Likes Page
Check If String Contains a Value in Array
How to Write Cdata Using Simplexmlelement
How to Use Spl_Autoload() Instead of _Autoload()
How to Delete an Object's Property in PHP
PHP Header(Location: ...): Force Url Change in Address Bar
What Is a Unix Timestamp and Why Use It
How to Read Only 5 Last Line of the Text File in PHP
Generating Xml Document in PHP (Escape Characters)
How to Modify Xml File Using PHP
How to Read a List of Files from a Folder Using PHP
What's the Difference Between PHP's Dom and Simplexml Extensions
How to Generate a Random Key Within PHP