Jump to content

array to string but hard return between words


kalster

Recommended Posts

 hello, i seem to be stuck again and this bug is difficult for me to solve.

if(isset($_POST['boards']))
	$boards[] = $_POST['boards'];
$allboards = implode("",$boards[0]);
echo $allboards;

outputs (browser source)...
hello
world

I have a combobox with the "multiple" property.  when two items are selected, the combobox "name" property has an array that passes to the $_POST variable. what i would like is those two words to be displayed on the same line.
I have tried rtrim($var, "\r\n") but that does nothing.

the reason i need them on the same line is because i would change then to "hello, world" and then add them as a mysql query, for example... $query = "SELECT hello, world FROM table...

Not sure why making that an array and back, this could work.

if(isset($_POST['boards']) && trim($_POST['boards']) != ''){//check if is set and not empty
$boards = trim($_POST['boards']);//trim end whitespace
$boards = preg_replace('/\s+/', ' ',$boards);//remove multiple spaces
$boards = str_replace(" ",",",$boards);//replace space with comma
echo $boards;
}

Unless you mean something like this with multiple checkboxes

<form method="POST" >
one: <input type="checkbox" name="boards[]" value="one"/>
two: <input type="checkbox" name="boards[]" value="two"/>
three: <input type="checkbox" name="boards[]" value="three"/>
<input type="submit" name="search" value="Search"/>
</form>
<?php
if(isset($_POST['boards']) && trim($_POST['boards']) != ''){//check if is set and not empty
$allboards = implode(",",$_POST['boards']);
echo $allboards;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.