Php, Add a Newline with Implode

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

How to implode an array to new lines

If you are using echo to display the imploded array, use <br> instead of \n

echo implode( "<br>", $array );

Since PHP renders echo outputs as HTML markups, whereas if you use \n, and view the page source, you will be able to see the words printed on separate lines in the page source instead of on the actual webpage.

Implode() array and make new line after two elements

You can use foreach to achive it, im pasting code for you which will give you your desired output

<?php
$labels = array("Element1", "Element2", "Element3", "Element4", "Element5","Element6");
$key = 1;
$lastkey = sizeof($labels);
foreach($labels as $value)
{
if($key%2)
{
if($key==$lastkey)
{
echo $value;
}
else
{
echo $value.",</br>";
}
}
else
{
if($key==$lastkey)
{
echo $value."</br>";
}
else
{
echo $value.",</br>";
}
}
$key++;
}
?>

How to implode newlines/break lines to a space?

You may want to use preg_replace for this instead of explode/implode:

$s = 'The quick brown
fox jumps
over the lazy
dog';

$s = preg_replace('#[\r\n]#', ' ', $s);

echo $s;

Output:

The quick brown fox jumps over the lazy dog

PHP: implode('\n', $appArray) generates extra '\'

Are you sure you're not intending: implode("\n", $appArray)? Newline characters aren't actually treated as newline characters when encapsulated in 'single quotes'.

Laravel implode array items to new lines

Displaying Unescaped Data

You can use <br> in the implode function and By default, Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax {!! !!}:

{!! $photo->tags->pluck('tag')->implode(",<br>") !!}

Output:

php,

css,

html

put contents of array into string with new line php

Try the implode function.

$input = file_get_contents('/srv/test.m3u');
$input = explode("\n", $input);

$result = array_unique($input);

$newresult = implode("\n",$result);

echo($newresult);


Related Topics



Leave a reply



Submit